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

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

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Unit tests for eliding and formatting utility functions. 5 // Unit tests for eliding and formatting utility functions.
6 6
7 #include "ui/gfx/text_elider.h" 7 #include "ui/gfx/text_elider.h"
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/font.h" 15 #include "ui/gfx/font.h"
16 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/text_utils.h"
16 #include "url/gurl.h" 18 #include "url/gurl.h"
17 19
18 namespace gfx { 20 namespace gfx {
19 21
20 namespace { 22 namespace {
21 23
22 struct Testcase { 24 struct Testcase {
23 const std::string input; 25 const std::string input;
24 const std::string output; 26 const std::string output;
25 }; 27 };
26 28
27 struct FileTestcase { 29 struct FileTestcase {
28 const base::FilePath::StringType input; 30 const base::FilePath::StringType input;
29 const std::string output; 31 const std::string output;
30 }; 32 };
31 33
32 struct UTF16Testcase { 34 struct UTF16Testcase {
33 const string16 input; 35 const string16 input;
34 const string16 output; 36 const string16 output;
35 }; 37 };
36 38
37 struct TestData { 39 struct TestData {
38 const std::string a; 40 const std::string a;
39 const std::string b; 41 const std::string b;
40 const int compare_result; 42 const int compare_result;
41 }; 43 };
42 44
45 float GetStringWidth(const string16& string, const Font& font) {
msw 2013/09/27 21:54:48 All the callsites for this function should constru
jianli 2013/10/01 00:32:58 Done.
46 gfx::FontList font_list(font);
msw 2013/09/27 21:54:48 nit: inline this, or make it const.
jianli 2013/10/01 00:32:58 Not needed. Removed.
47 return gfx::GetStringWidth(string, font_list);
48 }
49
43 void RunUrlTest(Testcase* testcases, size_t num_testcases) { 50 void RunUrlTest(Testcase* testcases, size_t num_testcases) {
44 static const gfx::Font font; 51 static const gfx::Font font;
45 for (size_t i = 0; i < num_testcases; ++i) { 52 for (size_t i = 0; i < num_testcases; ++i) {
46 const GURL url(testcases[i].input); 53 const GURL url(testcases[i].input);
47 // Should we test with non-empty language list? 54 // Should we test with non-empty language list?
48 // That's kinda redundant with net_util_unittests. 55 // That's kinda redundant with net_util_unittests.
49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), 56 EXPECT_EQ(UTF8ToUTF16(testcases[i].output),
50 ElideUrl(url, font, 57 ElideUrl(url, font,
51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), 58 GetStringWidth(UTF8ToUTF16(testcases[i].output), font),
52 std::string())); 59 std::string()));
53 } 60 }
54 } 61 }
55 62
56 gfx::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
70 } // namespace 63 } // namespace
71 64
72 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. 65 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth
73 // See crbug.com/154019 66 // that calls [NSString sizeWithFont] returns the rounded string width.
74 #if defined(OS_IOS) 67 #if defined(OS_IOS)
75 #define MAYBE_ElideEmail DISABLED_ElideEmail 68 #define MAYBE_ElideEmail DISABLED_ElideEmail
76 #else 69 #else
77 #define MAYBE_ElideEmail ElideEmail 70 #define MAYBE_ElideEmail ElideEmail
78 #endif 71 #endif
79 TEST(TextEliderTest, MAYBE_ElideEmail) { 72 TEST(TextEliderTest, MAYBE_ElideEmail) {
80 const std::string kEllipsisStr(kEllipsis); 73 const std::string kEllipsisStr(kEllipsis);
81 74
82 // Test emails and their expected elided forms (from which the available 75 // Test emails and their expected elided forms (from which the available
83 // widths will be derived). 76 // widths will be derived).
(...skipping 30 matching lines...) Expand all
114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, 107 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"},
115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", 108 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com",
116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, 109 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"},
117 // Special case: "m..." takes more than half of the available width; thus 110 // 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 111 // 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 112 // space for the minimal username elision although its half of the
120 // available width would normally allow it to elide to "l...l". 113 // available width would normally allow it to elide to "l...l".
121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, 114 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr},
122 }; 115 };
123 116
124 const gfx::Font font = GetTestingFont(); 117 const gfx::Font font;
125 for (size_t i = 0; i < arraysize(testcases); ++i) { 118 for (size_t i = 0; i < arraysize(testcases); ++i) {
126 const string16 expected_output = UTF8ToUTF16(testcases[i].output); 119 const string16 expected_output = UTF8ToUTF16(testcases[i].output);
127 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
136 EXPECT_EQ(expected_output, 120 EXPECT_EQ(expected_output,
137 ElideEmail(UTF8ToUTF16(testcases[i].input), 121 ElideEmail(UTF8ToUTF16(testcases[i].input),
138 font, 122 font,
139 available_width)); 123 GetStringWidth(expected_output, font)));
140 } 124 }
141 } 125 }
142 126
143 TEST(TextEliderTest, ElideEmailMoreSpace) { 127 TEST(TextEliderTest, ElideEmailMoreSpace) {
144 const int test_width_factors[] = { 128 const int test_width_factors[] = {
145 100, 129 100,
146 10000, 130 10000,
147 1000000, 131 1000000,
148 }; 132 };
149 const std::string test_emails[] = { 133 const std::string test_emails[] = {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // When there is very little space available, the elision code will shorten 178 // 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, 179 // 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 180 // there is a hack in place that simply treats them as one string in this
197 // case. 181 // case.
198 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { 182 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
199 const std::string kEllipsisStr(kEllipsis); 183 const std::string kEllipsisStr(kEllipsis);
200 184
201 // Very little space, would cause double ellipsis. 185 // Very little space, would cause double ellipsis.
202 gfx::Font font; 186 gfx::Font font;
203 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); 187 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
204 int available_width = font.GetStringWidth( 188 float available_width = GetStringWidth(
205 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); 189 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
190 font);
206 191
207 // Create the expected string, after elision. Depending on font size, the 192 // Create the expected string, after elision. Depending on font size, the
208 // directory might become /dir... or /di... or/d... - it never should be 193 // 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 194 // shorter than that. (If it is, the font considers d... to be longer
210 // than .../... - that should never happen). 195 // than .../... - that should never happen).
211 ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)), 196 ASSERT_GT(
212 font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr))); 197 GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), font),
198 GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr), font));
213 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); 199 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
214 string16 expected = ElideUrl(long_url, font, available_width, std::string()); 200 string16 expected = ElideUrl(long_url, font, available_width, std::string());
215 // Ensure that the expected result still contains part of the directory name. 201 // Ensure that the expected result still contains part of the directory name.
216 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); 202 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
217 EXPECT_EQ(expected, 203 EXPECT_EQ(expected,
218 ElideUrl(url, font, available_width, std::string())); 204 ElideUrl(url, font, available_width, std::string()));
219 205
220 // More space available - elide directories, partially elide filename. 206 // More space available - elide directories, partially elide filename.
221 Testcase testcases[] = { 207 Testcase testcases[] = {
222 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", 208 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 "C:/" + kEllipsisStr + "/filename"}, 267 "C:/" + kEllipsisStr + "/filename"},
282 #endif 268 #endif
283 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, 269 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
284 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, 270 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
285 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, 271 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
286 }; 272 };
287 273
288 RunUrlTest(testcases, arraysize(testcases)); 274 RunUrlTest(testcases, arraysize(testcases));
289 } 275 }
290 276
291 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. 277 // TODO(ios): This test fails on iOS because iOS version of gfx::GetStringWidth
278 // that calls [NSString sizeWithFont] returns the rounded string width.
292 // See crbug.com/154019 279 // See crbug.com/154019
293 #if defined(OS_IOS) 280 #if defined(OS_IOS)
294 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding 281 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
295 #else 282 #else
296 #define MAYBE_TestFilenameEliding TestFilenameEliding 283 #define MAYBE_TestFilenameEliding TestFilenameEliding
297 #endif 284 #endif
298 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { 285 TEST(TextEliderTest, MAYBE_TestFilenameEliding) {
299 const std::string kEllipsisStr(kEllipsis); 286 const std::string kEllipsisStr(kEllipsis);
300 const base::FilePath::StringType kPathSeparator = 287 const base::FilePath::StringType kPathSeparator =
301 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); 288 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]);
(...skipping 25 matching lines...) Expand all
327 {FILE_PATH_LITERAL("filename.middleext.longext"), 314 {FILE_PATH_LITERAL("filename.middleext.longext"),
328 "filename.mid" + kEllipsisStr + ".longext"}, 315 "filename.mid" + kEllipsisStr + ".longext"},
329 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), 316 {FILE_PATH_LITERAL("filename.superduperextremelylongext"),
330 "filename.sup" + kEllipsisStr + "emelylongext"}, 317 "filename.sup" + kEllipsisStr + "emelylongext"},
331 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), 318 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"),
332 "filenamereall" + kEllipsisStr + "emelylongext"}, 319 "filenamereall" + kEllipsisStr + "emelylongext"},
333 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), 320 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"),
334 "file.name.re" + kEllipsisStr + "emelylongext"} 321 "file.name.re" + kEllipsisStr + "emelylongext"}
335 }; 322 };
336 323
337 static const gfx::Font font = GetTestingFont(); 324 static const gfx::Font font;
338 for (size_t i = 0; i < arraysize(testcases); ++i) { 325 for (size_t i = 0; i < arraysize(testcases); ++i) {
339 base::FilePath filepath(testcases[i].input); 326 base::FilePath filepath(testcases[i].input);
340 string16 expected = UTF8ToUTF16(testcases[i].output); 327 string16 expected = UTF8ToUTF16(testcases[i].output);
341 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); 328 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected);
342 int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output)); 329 float available_width = GetStringWidth(UTF8ToUTF16(testcases[i].output),
343 #if defined(OS_MACOSX) 330 font);
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)); 331 EXPECT_EQ(expected, ElideFilename(filepath, font, available_width));
350 } 332 }
351 } 333 }
352 334
353 TEST(TextEliderTest, ElideTextTruncate) { 335 TEST(TextEliderTest, ElideTextTruncate) {
354 const gfx::Font font; 336 const gfx::Font font;
355 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); 337 const float kTestWidth = GetStringWidth(ASCIIToUTF16("Test"), font);
356 struct TestData { 338 struct TestData {
357 const char* input; 339 const char* input;
358 int width; 340 float width;
359 const char* output; 341 const char* output;
360 } cases[] = { 342 } cases[] = {
361 { "", 0, "" }, 343 { "", 0, "" },
362 { "Test", 0, "" }, 344 { "Test", 0, "" },
363 { "", kTestWidth, "" }, 345 { "", kTestWidth, "" },
364 { "Tes", kTestWidth, "Tes" }, 346 { "Tes", kTestWidth, "Tes" },
365 { "Test", kTestWidth, "Test" }, 347 { "Test", kTestWidth, "Test" },
366 { "Tests", kTestWidth, "Test" }, 348 { "Tests", kTestWidth, "Test" },
367 }; 349 };
368 350
369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 351 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
370 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, 352 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font,
371 cases[i].width, TRUNCATE_AT_END); 353 cases[i].width, TRUNCATE_AT_END);
372 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); 354 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result));
373 } 355 }
374 } 356 }
375 357
376 TEST(TextEliderTest, ElideTextEllipsis) { 358 TEST(TextEliderTest, ElideTextEllipsis) {
377 const gfx::Font font; 359 const gfx::Font font;
378 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); 360 const float kTestWidth = GetStringWidth(ASCIIToUTF16("Test"), font);
379 const char* kEllipsis = "\xE2\x80\xA6"; 361 const char* kEllipsis = "\xE2\x80\xA6";
380 const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); 362 const float kEllipsisWidth = GetStringWidth(UTF8ToUTF16(kEllipsis), font);
381 struct TestData { 363 struct TestData {
382 const char* input; 364 const char* input;
383 int width; 365 float width;
384 const char* output; 366 const char* output;
385 } cases[] = { 367 } cases[] = {
386 { "", 0, "" }, 368 { "", 0, "" },
387 { "Test", 0, "" }, 369 { "Test", 0, "" },
388 { "Test", kEllipsisWidth, kEllipsis }, 370 { "Test", kEllipsisWidth, kEllipsis },
389 { "", kTestWidth, "" }, 371 { "", kTestWidth, "" },
390 { "Tes", kTestWidth, "Tes" }, 372 { "Tes", kTestWidth, "Tes" },
391 { "Test", kTestWidth, "Test" }, 373 { "Test", kTestWidth, "Test" },
392 }; 374 };
393 375
(...skipping 23 matching lines...) Expand all
417 } 399 }
418 } 400 }
419 401
420 TEST(TextEliderTest, ElideTextSurrogatePairs) { 402 TEST(TextEliderTest, ElideTextSurrogatePairs) {
421 const gfx::Font font; 403 const gfx::Font font;
422 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as 404 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
423 // two characters forming a surrogate pair 0x0001D11E. 405 // two characters forming a surrogate pair 0x0001D11E.
424 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; 406 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
425 const string16 kTestString = 407 const string16 kTestString =
426 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); 408 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd");
427 const int kTestStringWidth = font.GetStringWidth(kTestString); 409 const float kTestStringWidth = GetStringWidth(kTestString, font);
428 const char16 kSurrogateFirstChar = kTestString[0]; 410 const char16 kSurrogateFirstChar = kTestString[0];
429 const char16 kSurrogateSecondChar = kTestString[1]; 411 const char16 kSurrogateSecondChar = kTestString[1];
430 string16 result; 412 string16 result;
431 413
432 // Elide |kTextString| to all possible widths and check that no instance of 414 // Elide |kTextString| to all possible widths and check that no instance of
433 // |kSurrogate| was split in two. 415 // |kSurrogate| was split in two.
434 for (int width = 0; width <= kTestStringWidth; width++) { 416 for (float width = 0; width <= kTestStringWidth; width += 1) {
msw 2013/09/27 21:54:48 nit: ++width or leave this as it was. Why "+=1"?
jianli 2013/10/01 00:32:58 I though this did not work for float. It turned ou
435 result = ElideText(kTestString, font, width, TRUNCATE_AT_END); 417 result = ElideText(kTestString, font, width, TRUNCATE_AT_END);
436 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); 418 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
437 419
438 result = ElideText(kTestString, font, width, ELIDE_AT_END); 420 result = ElideText(kTestString, font, width, ELIDE_AT_END);
439 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); 421 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
440 422
441 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); 423 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE);
442 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); 424 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar);
443 } 425 }
444 } 426 }
(...skipping 16 matching lines...) Expand all
461 UTF16Testcase testcases_end[] = { 443 UTF16Testcase testcases_end[] = {
462 {data_scheme + ten_a, data_scheme + ten_a}, 444 {data_scheme + ten_a, data_scheme + ten_a},
463 {data_scheme + hundred_a, data_scheme + hundred_a}, 445 {data_scheme + hundred_a, data_scheme + hundred_a},
464 {data_scheme + thousand_a, long_string_end}, 446 {data_scheme + thousand_a, long_string_end},
465 {data_scheme + ten_thousand_a, long_string_end}, 447 {data_scheme + ten_thousand_a, long_string_end},
466 {data_scheme + hundred_thousand_a, long_string_end}, 448 {data_scheme + hundred_thousand_a, long_string_end},
467 {data_scheme + million_a, long_string_end}, 449 {data_scheme + million_a, long_string_end},
468 }; 450 };
469 451
470 const gfx::Font font; 452 const gfx::Font font;
471 int ellipsis_width = font.GetStringWidth(kEllipsisStr); 453 float ellipsis_width = gfx::GetStringWidth(kEllipsisStr, FontList(font));
472 for (size_t i = 0; i < arraysize(testcases_end); ++i) { 454 for (size_t i = 0; i < arraysize(testcases_end); ++i) {
473 // Compare sizes rather than actual contents because if the test fails, 455 // Compare sizes rather than actual contents because if the test fails,
474 // output is rather long. 456 // output is rather long.
475 EXPECT_EQ(testcases_end[i].output.size(), 457 EXPECT_EQ(testcases_end[i].output.size(),
476 ElideText(testcases_end[i].input, font, 458 ElideText(testcases_end[i].input, font,
477 font.GetStringWidth(testcases_end[i].output), 459 GetStringWidth(testcases_end[i].output, font),
478 ELIDE_AT_END).size()); 460 ELIDE_AT_END).size());
479 EXPECT_EQ(kEllipsisStr, 461 EXPECT_EQ(kEllipsisStr,
480 ElideText(testcases_end[i].input, font, ellipsis_width, 462 ElideText(testcases_end[i].input, font, ellipsis_width,
481 ELIDE_AT_END)); 463 ELIDE_AT_END));
482 } 464 }
483 465
484 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; 466 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2;
485 string16 long_string_middle(data_scheme + 467 string16 long_string_middle(data_scheme +
486 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + 468 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr +
487 string16(number_of_trailing_as, 'a')); 469 string16(number_of_trailing_as, 'a'));
488 UTF16Testcase testcases_middle[] = { 470 UTF16Testcase testcases_middle[] = {
489 {data_scheme + ten_a, data_scheme + ten_a}, 471 {data_scheme + ten_a, data_scheme + ten_a},
490 {data_scheme + hundred_a, data_scheme + hundred_a}, 472 {data_scheme + hundred_a, data_scheme + hundred_a},
491 {data_scheme + thousand_a, long_string_middle}, 473 {data_scheme + thousand_a, long_string_middle},
492 {data_scheme + ten_thousand_a, long_string_middle}, 474 {data_scheme + ten_thousand_a, long_string_middle},
493 {data_scheme + hundred_thousand_a, long_string_middle}, 475 {data_scheme + hundred_thousand_a, long_string_middle},
494 {data_scheme + million_a, long_string_middle}, 476 {data_scheme + million_a, long_string_middle},
495 }; 477 };
496 478
497 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { 479 for (size_t i = 0; i < arraysize(testcases_middle); ++i) {
498 // Compare sizes rather than actual contents because if the test fails, 480 // Compare sizes rather than actual contents because if the test fails,
499 // output is rather long. 481 // output is rather long.
500 EXPECT_EQ(testcases_middle[i].output.size(), 482 EXPECT_EQ(testcases_middle[i].output.size(),
501 ElideText(testcases_middle[i].input, font, 483 ElideText(testcases_middle[i].input, font,
502 font.GetStringWidth(testcases_middle[i].output), 484 GetStringWidth(testcases_middle[i].output, font),
503 ELIDE_AT_END).size()); 485 ELIDE_AT_END).size());
504 EXPECT_EQ(kEllipsisStr, 486 EXPECT_EQ(kEllipsisStr,
505 ElideText(testcases_middle[i].input, font, ellipsis_width, 487 ElideText(testcases_middle[i].input, font, ellipsis_width,
506 ELIDE_AT_END)); 488 ELIDE_AT_END));
507 } 489 }
508 } 490 }
509 491
510 // Verifies display_url is set correctly. 492 // Verifies display_url is set correctly.
511 TEST(TextEliderTest, SortedDisplayURL) { 493 TEST(TextEliderTest, SortedDisplayURL) {
512 SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); 494 SortedDisplayURL d_url(GURL("http://www.google.com"), std::string());
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 string16 output; 556 string16 output;
575 EXPECT_EQ(cases[i].result, 557 EXPECT_EQ(cases[i].result,
576 ElideString(UTF8ToUTF16(cases[i].input), 558 ElideString(UTF8ToUTF16(cases[i].input),
577 cases[i].max_len, &output)); 559 cases[i].max_len, &output));
578 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); 560 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output));
579 } 561 }
580 } 562 }
581 563
582 TEST(TextEliderTest, ElideRectangleText) { 564 TEST(TextEliderTest, ElideRectangleText) {
583 const gfx::Font font; 565 const gfx::Font font;
584 const int line_height = font.GetHeight(); 566 const float line_height = font.GetHeight();
585 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); 567 const float test_width = GetStringWidth(ASCIIToUTF16("Test"), font);
586 568
587 struct TestData { 569 struct TestData {
588 const char* input; 570 const char* input;
589 int available_pixel_width; 571 float available_pixel_width;
590 int available_pixel_height; 572 float available_pixel_height;
591 bool truncated_y; 573 bool truncated_y;
592 const char* output; 574 const char* output;
593 } cases[] = { 575 } cases[] = {
594 { "", 0, 0, false, NULL }, 576 { "", 0, 0, false, NULL },
595 { "", 1, 1, false, NULL }, 577 { "", 1, 1, false, NULL },
596 { "Test", test_width, 0, true, NULL }, 578 { "Test", test_width, 0, true, NULL },
597 { "Test", test_width, 1, false, "Test" }, 579 { "Test", test_width, 1, false, "Test" },
598 { "Test", test_width, line_height, false, "Test" }, 580 { "Test", test_width, line_height, false, "Test" },
599 { "Test Test", test_width, line_height, true, "Test" }, 581 { "Test Test", test_width, line_height, true, "Test" },
600 { "Test Test", test_width, line_height + 1, false, "Test|Test" }, 582 { "Test Test", test_width, line_height + 1, false, "Test|Test" },
(...skipping 30 matching lines...) Expand all
631 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); 613 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
632 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; 614 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
633 } else { 615 } else {
634 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; 616 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
635 } 617 }
636 } 618 }
637 } 619 }
638 620
639 TEST(TextEliderTest, ElideRectangleTextPunctuation) { 621 TEST(TextEliderTest, ElideRectangleTextPunctuation) {
640 const gfx::Font font; 622 const gfx::Font font;
641 const int line_height = font.GetHeight(); 623 const float line_height = font.GetHeight();
642 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); 624 const float test_width = GetStringWidth(ASCIIToUTF16("Test"), font);
643 const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); 625 const float test_t_width = GetStringWidth(ASCIIToUTF16("Test T"), font);
644 626
645 struct TestData { 627 struct TestData {
646 const char* input; 628 const char* input;
647 int available_pixel_width; 629 float available_pixel_width;
648 int available_pixel_height; 630 float available_pixel_height;
649 bool wrap_words; 631 bool wrap_words;
650 bool truncated_x; 632 bool truncated_x;
651 const char* output; 633 const char* output;
652 } cases[] = { 634 } cases[] = {
653 { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, 635 { "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 ?" }, 636 { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" },
655 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" }, 637 { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" },
656 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, 638 { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" },
657 }; 639 };
658 640
(...skipping 12 matching lines...) Expand all
671 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); 653 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
672 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; 654 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!";
673 } else { 655 } else {
674 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; 656 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!";
675 } 657 }
676 } 658 }
677 } 659 }
678 660
679 TEST(TextEliderTest, ElideRectangleTextLongWords) { 661 TEST(TextEliderTest, ElideRectangleTextLongWords) {
680 const gfx::Font font; 662 const gfx::Font font;
681 const int kAvailableHeight = 1000; 663 const float kAvailableHeight = 1000;
682 const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis); 664 const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis);
683 const int elided_width = font.GetStringWidth(kElidedTesting); 665 const float elided_width = GetStringWidth(kElidedTesting, font);
684 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); 666 const float test_width = GetStringWidth(ASCIIToUTF16("Test"), font);
685 667
686 struct TestData { 668 struct TestData {
687 const char* input; 669 const char* input;
688 int available_pixel_width; 670 float available_pixel_width;
689 WordWrapBehavior wrap_behavior; 671 WordWrapBehavior wrap_behavior;
690 bool truncated_x; 672 bool truncated_x;
691 const char* output; 673 const char* output;
692 } cases[] = { 674 } cases[] = {
693 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" }, 675 { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" },
694 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" }, 676 { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" },
695 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, 677 { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
696 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, 678 { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" },
697 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" }, 679 { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" },
698 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" }, 680 { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" },
(...skipping 29 matching lines...) Expand all
728 kAvailableHeight, 710 kAvailableHeight,
729 cases[i].wrap_behavior, 711 cases[i].wrap_behavior,
730 &lines)); 712 &lines));
731 std::string expected_output(cases[i].output); 713 std::string expected_output(cases[i].output);
732 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); 714 ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis);
733 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); 715 const std::string result = UTF16ToUTF8(JoinString(lines, '|'));
734 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; 716 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!";
735 } 717 }
736 } 718 }
737 719
738 // 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 720 // 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 721 // 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 722 // fail because the truncated integer width is returned for the string
750 // and the accumulation of the truncated values causes the elide function 723 // and the accumulation of the truncated values causes the elide function
751 // to wrap incorrectly. 724 // to wrap incorrectly.
752 TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) { 725 TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) {
753 gfx::Font font = GetTestingFont(); 726 gfx::Font font;
754 const int kAvailableWidth = 235; 727 #if defined(OS_MACOSX) && !defined(OS_IOS)
755 const int kAvailableHeight = 1000; 728 // Use a specific font to expose the line width exceeding problem.
729 font = gfx::Font("LucidaGrande", 12);
730 #endif
731 const float kAvailableWidth = 235;
732 const float kAvailableHeight = 1000;
756 const char text[] = "that Russian place we used to go to after fencing"; 733 const char text[] = "that Russian place we used to go to after fencing";
757 std::vector<string16> lines; 734 std::vector<string16> lines;
758 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), 735 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text),
759 font, 736 font,
760 kAvailableWidth, 737 kAvailableWidth,
761 kAvailableHeight, 738 kAvailableHeight,
762 WRAP_LONG_WORDS, 739 WRAP_LONG_WORDS,
763 &lines)); 740 &lines));
764 ASSERT_EQ(2u, lines.size()); 741 ASSERT_EQ(2u, lines.size());
765 EXPECT_LE(font.GetStringWidth(lines[0]), kAvailableWidth); 742 EXPECT_LE(GetStringWidth(lines[0], font), kAvailableWidth);
766 EXPECT_LE(font.GetStringWidth(lines[1]), kAvailableWidth); 743 EXPECT_LE(GetStringWidth(lines[1], font), kAvailableWidth);
767 } 744 }
768 745
769 TEST(TextEliderTest, ElideRectangleString) { 746 TEST(TextEliderTest, ElideRectangleString) {
770 struct TestData { 747 struct TestData {
771 const char* input; 748 const char* input;
772 int max_rows; 749 int max_rows;
773 int max_cols; 750 int max_cols;
774 bool result; 751 bool result;
775 const char* output; 752 const char* output;
776 } cases[] = { 753 } cases[] = {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 961
985 // Test adds ... at right spot when there is not enough room to break at a 962 // Test adds ... at right spot when there is not enough room to break at a
986 // word boundary. 963 // word boundary.
987 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); 964 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11)));
988 965
989 // Test completely truncates string if break is on initial whitespace. 966 // Test completely truncates string if break is on initial whitespace.
990 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); 967 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2)));
991 } 968 }
992 969
993 } // namespace gfx 970 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698