| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Unit tests for eliding and formatting utility functions. | 5 // Unit tests for eliding and formatting utility functions. |
| 6 | 6 |
| 7 #include "ui/gfx/text_elider.h" | 7 #include "ui/gfx/text_elider.h" |
| 8 | 8 |
| 9 #include "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 |
| 43 void RunUrlTest(Testcase* testcases, size_t num_testcases) { | 45 void RunUrlTest(Testcase* testcases, size_t num_testcases) { |
| 44 static const gfx::Font font; | 46 static const FontList font_list; |
| 45 for (size_t i = 0; i < num_testcases; ++i) { | 47 for (size_t i = 0; i < num_testcases; ++i) { |
| 46 const GURL url(testcases[i].input); | 48 const GURL url(testcases[i].input); |
| 47 // Should we test with non-empty language list? | 49 // Should we test with non-empty language list? |
| 48 // That's kinda redundant with net_util_unittests. | 50 // That's kinda redundant with net_util_unittests. |
| 51 const float available_width = |
| 52 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); |
| 49 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), | 53 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), |
| 50 ElideUrl(url, font, | 54 ElideUrl(url, font_list, available_width, std::string())); |
| 51 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), | |
| 52 std::string())); | |
| 53 } | 55 } |
| 54 } | 56 } |
| 55 | 57 |
| 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 | 58 } // namespace |
| 71 | 59 |
| 72 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 60 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 73 // See crbug.com/154019 | 61 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 74 #if defined(OS_IOS) | 62 #if defined(OS_IOS) |
| 75 #define MAYBE_ElideEmail DISABLED_ElideEmail | 63 #define MAYBE_ElideEmail DISABLED_ElideEmail |
| 76 #else | 64 #else |
| 77 #define MAYBE_ElideEmail ElideEmail | 65 #define MAYBE_ElideEmail ElideEmail |
| 78 #endif | 66 #endif |
| 79 TEST(TextEliderTest, MAYBE_ElideEmail) { | 67 TEST(TextEliderTest, MAYBE_ElideEmail) { |
| 80 const std::string kEllipsisStr(kEllipsis); | 68 const std::string kEllipsisStr(kEllipsis); |
| 81 | 69 |
| 82 // Test emails and their expected elided forms (from which the available | 70 // Test emails and their expected elided forms (from which the available |
| 83 // widths will be derived). | 71 // widths will be derived). |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, | 102 "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, |
| 115 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", | 103 {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", |
| 116 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, | 104 "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, |
| 117 // Special case: "m..." takes more than half of the available width; thus | 105 // 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 | 106 // 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 | 107 // space for the minimal username elision although its half of the |
| 120 // available width would normally allow it to elide to "l...l". | 108 // available width would normally allow it to elide to "l...l". |
| 121 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, | 109 {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, |
| 122 }; | 110 }; |
| 123 | 111 |
| 124 const gfx::Font font = GetTestingFont(); | 112 const FontList font_list; |
| 125 for (size_t i = 0; i < arraysize(testcases); ++i) { | 113 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 126 const string16 expected_output = UTF8ToUTF16(testcases[i].output); | 114 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, | 115 EXPECT_EQ(expected_output, |
| 137 ElideEmail(UTF8ToUTF16(testcases[i].input), | 116 ElideEmail( |
| 138 font, | 117 UTF8ToUTF16(testcases[i].input), |
| 139 available_width)); | 118 font_list, |
| 119 GetStringWidthF(expected_output, font_list))); |
| 140 } | 120 } |
| 141 } | 121 } |
| 142 | 122 |
| 143 TEST(TextEliderTest, ElideEmailMoreSpace) { | 123 TEST(TextEliderTest, ElideEmailMoreSpace) { |
| 144 const int test_width_factors[] = { | 124 const int test_width_factors[] = { |
| 145 100, | 125 100, |
| 146 10000, | 126 10000, |
| 147 1000000, | 127 1000000, |
| 148 }; | 128 }; |
| 149 const std::string test_emails[] = { | 129 const std::string test_emails[] = { |
| 150 "a@c", | 130 "a@c", |
| 151 "test@email.com", | 131 "test@email.com", |
| 152 "short@verysuperdupperlongdomain.com", | 132 "short@verysuperdupperlongdomain.com", |
| 153 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", | 133 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", |
| 154 }; | 134 }; |
| 155 | 135 |
| 156 const gfx::Font font; | 136 const Font font; |
| 157 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { | 137 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
| 158 const int test_width = test_width_factors[i] * | 138 const int test_width = test_width_factors[i] * |
| 159 font.GetAverageCharacterWidth(); | 139 font.GetAverageCharacterWidth(); |
| 160 for (size_t j = 0; j < arraysize(test_emails); ++j) { | 140 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 161 // Extra space is available: the email should not be elided. | 141 // Extra space is available: the email should not be elided. |
| 162 const string16 test_email = UTF8ToUTF16(test_emails[j]); | 142 const string16 test_email = UTF8ToUTF16(test_emails[j]); |
| 163 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); | 143 EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); |
| 164 } | 144 } |
| 165 } | 145 } |
| 166 } | 146 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 192 } | 172 } |
| 193 | 173 |
| 194 // When there is very little space available, the elision code will shorten | 174 // 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, | 175 // 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 | 176 // there is a hack in place that simply treats them as one string in this |
| 197 // case. | 177 // case. |
| 198 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { | 178 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
| 199 const std::string kEllipsisStr(kEllipsis); | 179 const std::string kEllipsisStr(kEllipsis); |
| 200 | 180 |
| 201 // Very little space, would cause double ellipsis. | 181 // Very little space, would cause double ellipsis. |
| 202 gfx::Font font; | 182 FontList font_list; |
| 203 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); | 183 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
| 204 int available_width = font.GetStringWidth( | 184 float available_width = GetStringWidthF( |
| 205 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); | 185 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), |
| 186 font_list); |
| 206 | 187 |
| 207 // Create the expected string, after elision. Depending on font size, the | 188 // Create the expected string, after elision. Depending on font size, the |
| 208 // directory might become /dir... or /di... or/d... - it never should be | 189 // 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 | 190 // shorter than that. (If it is, the font considers d... to be longer |
| 210 // than .../... - that should never happen). | 191 // than .../... - that should never happen). |
| 211 ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)), | 192 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), |
| 212 font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr))); | 193 font_list), |
| 194 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); |
| 213 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); | 195 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
| 214 string16 expected = ElideUrl(long_url, font, available_width, std::string()); | 196 string16 expected = |
| 197 ElideUrl(long_url, font_list, available_width, std::string()); |
| 215 // Ensure that the expected result still contains part of the directory name. | 198 // Ensure that the expected result still contains part of the directory name. |
| 216 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); | 199 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
| 217 EXPECT_EQ(expected, | 200 EXPECT_EQ(expected, |
| 218 ElideUrl(url, font, available_width, std::string())); | 201 ElideUrl(url, font_list, available_width, std::string())); |
| 219 | 202 |
| 220 // More space available - elide directories, partially elide filename. | 203 // More space available - elide directories, partially elide filename. |
| 221 Testcase testcases[] = { | 204 Testcase testcases[] = { |
| 222 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", | 205 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", |
| 223 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, | 206 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, |
| 224 }; | 207 }; |
| 225 RunUrlTest(testcases, arraysize(testcases)); | 208 RunUrlTest(testcases, arraysize(testcases)); |
| 226 } | 209 } |
| 227 | 210 |
| 228 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. | 211 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 "C:/" + kEllipsisStr + "/filename"}, | 264 "C:/" + kEllipsisStr + "/filename"}, |
| 282 #endif | 265 #endif |
| 283 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, | 266 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, |
| 284 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, | 267 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, |
| 285 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, | 268 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, |
| 286 }; | 269 }; |
| 287 | 270 |
| 288 RunUrlTest(testcases, arraysize(testcases)); | 271 RunUrlTest(testcases, arraysize(testcases)); |
| 289 } | 272 } |
| 290 | 273 |
| 291 // TODO(ios): Complex eliding is off by one for some of those tests on iOS. | 274 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 292 // See crbug.com/154019 | 275 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 293 #if defined(OS_IOS) | 276 #if defined(OS_IOS) |
| 294 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 277 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
| 295 #else | 278 #else |
| 296 #define MAYBE_TestFilenameEliding TestFilenameEliding | 279 #define MAYBE_TestFilenameEliding TestFilenameEliding |
| 297 #endif | 280 #endif |
| 298 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { | 281 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
| 299 const std::string kEllipsisStr(kEllipsis); | 282 const std::string kEllipsisStr(kEllipsis); |
| 300 const base::FilePath::StringType kPathSeparator = | 283 const base::FilePath::StringType kPathSeparator = |
| 301 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); | 284 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); |
| 302 | 285 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 327 {FILE_PATH_LITERAL("filename.middleext.longext"), | 310 {FILE_PATH_LITERAL("filename.middleext.longext"), |
| 328 "filename.mid" + kEllipsisStr + ".longext"}, | 311 "filename.mid" + kEllipsisStr + ".longext"}, |
| 329 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), | 312 {FILE_PATH_LITERAL("filename.superduperextremelylongext"), |
| 330 "filename.sup" + kEllipsisStr + "emelylongext"}, | 313 "filename.sup" + kEllipsisStr + "emelylongext"}, |
| 331 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), | 314 {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), |
| 332 "filenamereall" + kEllipsisStr + "emelylongext"}, | 315 "filenamereall" + kEllipsisStr + "emelylongext"}, |
| 333 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), | 316 {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), |
| 334 "file.name.re" + kEllipsisStr + "emelylongext"} | 317 "file.name.re" + kEllipsisStr + "emelylongext"} |
| 335 }; | 318 }; |
| 336 | 319 |
| 337 static const gfx::Font font = GetTestingFont(); | 320 static const FontList font_list; |
| 338 for (size_t i = 0; i < arraysize(testcases); ++i) { | 321 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 339 base::FilePath filepath(testcases[i].input); | 322 base::FilePath filepath(testcases[i].input); |
| 340 string16 expected = UTF8ToUTF16(testcases[i].output); | 323 string16 expected = UTF8ToUTF16(testcases[i].output); |
| 341 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); | 324 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
| 342 int available_width = font.GetStringWidth(UTF8ToUTF16(testcases[i].output)); | 325 EXPECT_EQ(expected, ElideFilename(filepath, font_list, |
| 343 #if defined(OS_MACOSX) | 326 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list))); |
| 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)); | |
| 350 } | 327 } |
| 351 } | 328 } |
| 352 | 329 |
| 353 TEST(TextEliderTest, ElideTextTruncate) { | 330 TEST(TextEliderTest, ElideTextTruncate) { |
| 354 const gfx::Font font; | 331 const FontList font_list; |
| 355 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); | 332 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 356 struct TestData { | 333 struct TestData { |
| 357 const char* input; | 334 const char* input; |
| 358 int width; | 335 float width; |
| 359 const char* output; | 336 const char* output; |
| 360 } cases[] = { | 337 } cases[] = { |
| 361 { "", 0, "" }, | 338 { "", 0, "" }, |
| 362 { "Test", 0, "" }, | 339 { "Test", 0, "" }, |
| 363 { "", kTestWidth, "" }, | 340 { "", kTestWidth, "" }, |
| 364 { "Tes", kTestWidth, "Tes" }, | 341 { "Tes", kTestWidth, "Tes" }, |
| 365 { "Test", kTestWidth, "Test" }, | 342 { "Test", kTestWidth, "Test" }, |
| 366 { "Tests", kTestWidth, "Test" }, | 343 { "Tests", kTestWidth, "Test" }, |
| 367 }; | 344 }; |
| 368 | 345 |
| 369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 346 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 370 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, | 347 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
| 371 cases[i].width, TRUNCATE_AT_END); | 348 cases[i].width, TRUNCATE_AT_END); |
| 372 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 349 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 373 } | 350 } |
| 374 } | 351 } |
| 375 | 352 |
| 376 TEST(TextEliderTest, ElideTextEllipsis) { | 353 TEST(TextEliderTest, ElideTextEllipsis) { |
| 377 const gfx::Font font; | 354 const FontList font_list; |
| 378 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); | 355 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 379 const char* kEllipsis = "\xE2\x80\xA6"; | 356 const char* kEllipsis = "\xE2\x80\xA6"; |
| 380 const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); | 357 const float kEllipsisWidth = |
| 358 GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list); |
| 381 struct TestData { | 359 struct TestData { |
| 382 const char* input; | 360 const char* input; |
| 383 int width; | 361 float width; |
| 384 const char* output; | 362 const char* output; |
| 385 } cases[] = { | 363 } cases[] = { |
| 386 { "", 0, "" }, | 364 { "", 0, "" }, |
| 387 { "Test", 0, "" }, | 365 { "Test", 0, "" }, |
| 388 { "Test", kEllipsisWidth, kEllipsis }, | 366 { "Test", kEllipsisWidth, kEllipsis }, |
| 389 { "", kTestWidth, "" }, | 367 { "", kTestWidth, "" }, |
| 390 { "Tes", kTestWidth, "Tes" }, | 368 { "Tes", kTestWidth, "Tes" }, |
| 391 { "Test", kTestWidth, "Test" }, | 369 { "Test", kTestWidth, "Test" }, |
| 392 }; | 370 }; |
| 393 | 371 |
| 394 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 395 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, | 373 string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
| 396 cases[i].width, ELIDE_AT_END); | 374 cases[i].width, ELIDE_AT_END); |
| 397 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 375 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 398 } | 376 } |
| 399 } | 377 } |
| 400 | 378 |
| 401 // Checks that all occurrences of |first_char| are followed by |second_char| and | 379 // 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|. | 380 // all occurrences of |second_char| are preceded by |first_char| in |text|. |
| 403 static void CheckSurrogatePairs(const string16& text, | 381 static void CheckSurrogatePairs(const string16& text, |
| 404 char16 first_char, | 382 char16 first_char, |
| 405 char16 second_char) { | 383 char16 second_char) { |
| 406 size_t index = text.find_first_of(first_char); | 384 size_t index = text.find_first_of(first_char); |
| 407 while (index != string16::npos) { | 385 while (index != string16::npos) { |
| 408 EXPECT_LT(index, text.length() - 1); | 386 EXPECT_LT(index, text.length() - 1); |
| 409 EXPECT_EQ(second_char, text[index + 1]); | 387 EXPECT_EQ(second_char, text[index + 1]); |
| 410 index = text.find_first_of(first_char, index + 1); | 388 index = text.find_first_of(first_char, index + 1); |
| 411 } | 389 } |
| 412 index = text.find_first_of(second_char); | 390 index = text.find_first_of(second_char); |
| 413 while (index != string16::npos) { | 391 while (index != string16::npos) { |
| 414 EXPECT_GT(index, 0U); | 392 EXPECT_GT(index, 0U); |
| 415 EXPECT_EQ(first_char, text[index - 1]); | 393 EXPECT_EQ(first_char, text[index - 1]); |
| 416 index = text.find_first_of(second_char, index + 1); | 394 index = text.find_first_of(second_char, index + 1); |
| 417 } | 395 } |
| 418 } | 396 } |
| 419 | 397 |
| 420 TEST(TextEliderTest, ElideTextSurrogatePairs) { | 398 TEST(TextEliderTest, ElideTextSurrogatePairs) { |
| 421 const gfx::Font font; | 399 const FontList font_list; |
| 422 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as | 400 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
| 423 // two characters forming a surrogate pair 0x0001D11E. | 401 // two characters forming a surrogate pair 0x0001D11E. |
| 424 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; | 402 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
| 425 const string16 kTestString = | 403 const string16 kTestString = |
| 426 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | 404 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); |
| 427 const int kTestStringWidth = font.GetStringWidth(kTestString); | 405 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
| 428 const char16 kSurrogateFirstChar = kTestString[0]; | 406 const char16 kSurrogateFirstChar = kTestString[0]; |
| 429 const char16 kSurrogateSecondChar = kTestString[1]; | 407 const char16 kSurrogateSecondChar = kTestString[1]; |
| 430 string16 result; | 408 string16 result; |
| 431 | 409 |
| 432 // Elide |kTextString| to all possible widths and check that no instance of | 410 // Elide |kTextString| to all possible widths and check that no instance of |
| 433 // |kSurrogate| was split in two. | 411 // |kSurrogate| was split in two. |
| 434 for (int width = 0; width <= kTestStringWidth; width++) { | 412 for (float width = 0; width <= kTestStringWidth; width++) { |
| 435 result = ElideText(kTestString, font, width, TRUNCATE_AT_END); | 413 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); |
| 436 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 414 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 437 | 415 |
| 438 result = ElideText(kTestString, font, width, ELIDE_AT_END); | 416 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); |
| 439 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 417 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 440 | 418 |
| 441 result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); | 419 result = ElideText(kTestString, font_list, width, ELIDE_IN_MIDDLE); |
| 442 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 420 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 443 } | 421 } |
| 444 } | 422 } |
| 445 | 423 |
| 446 TEST(TextEliderTest, ElideTextLongStrings) { | 424 TEST(TextEliderTest, ElideTextLongStrings) { |
| 447 const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); | 425 const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); |
| 448 string16 data_scheme(UTF8ToUTF16("data:text/plain,")); | 426 string16 data_scheme(UTF8ToUTF16("data:text/plain,")); |
| 449 size_t data_scheme_length = data_scheme.length(); | 427 size_t data_scheme_length = data_scheme.length(); |
| 450 | 428 |
| 451 string16 ten_a(10, 'a'); | 429 string16 ten_a(10, 'a'); |
| 452 string16 hundred_a(100, 'a'); | 430 string16 hundred_a(100, 'a'); |
| 453 string16 thousand_a(1000, 'a'); | 431 string16 thousand_a(1000, 'a'); |
| 454 string16 ten_thousand_a(10000, 'a'); | 432 string16 ten_thousand_a(10000, 'a'); |
| 455 string16 hundred_thousand_a(100000, 'a'); | 433 string16 hundred_thousand_a(100000, 'a'); |
| 456 string16 million_a(1000000, 'a'); | 434 string16 million_a(1000000, 'a'); |
| 457 | 435 |
| 458 size_t number_of_as = 156; | 436 size_t number_of_as = 156; |
| 459 string16 long_string_end( | 437 string16 long_string_end( |
| 460 data_scheme + string16(number_of_as, 'a') + kEllipsisStr); | 438 data_scheme + string16(number_of_as, 'a') + kEllipsisStr); |
| 461 UTF16Testcase testcases_end[] = { | 439 UTF16Testcase testcases_end[] = { |
| 462 {data_scheme + ten_a, data_scheme + ten_a}, | 440 {data_scheme + ten_a, data_scheme + ten_a}, |
| 463 {data_scheme + hundred_a, data_scheme + hundred_a}, | 441 {data_scheme + hundred_a, data_scheme + hundred_a}, |
| 464 {data_scheme + thousand_a, long_string_end}, | 442 {data_scheme + thousand_a, long_string_end}, |
| 465 {data_scheme + ten_thousand_a, long_string_end}, | 443 {data_scheme + ten_thousand_a, long_string_end}, |
| 466 {data_scheme + hundred_thousand_a, long_string_end}, | 444 {data_scheme + hundred_thousand_a, long_string_end}, |
| 467 {data_scheme + million_a, long_string_end}, | 445 {data_scheme + million_a, long_string_end}, |
| 468 }; | 446 }; |
| 469 | 447 |
| 470 const gfx::Font font; | 448 const FontList font_list; |
| 471 int ellipsis_width = font.GetStringWidth(kEllipsisStr); | 449 float ellipsis_width = GetStringWidthF(kEllipsisStr, font_list); |
| 472 for (size_t i = 0; i < arraysize(testcases_end); ++i) { | 450 for (size_t i = 0; i < arraysize(testcases_end); ++i) { |
| 473 // Compare sizes rather than actual contents because if the test fails, | 451 // Compare sizes rather than actual contents because if the test fails, |
| 474 // output is rather long. | 452 // output is rather long. |
| 475 EXPECT_EQ(testcases_end[i].output.size(), | 453 EXPECT_EQ(testcases_end[i].output.size(), |
| 476 ElideText(testcases_end[i].input, font, | 454 ElideText( |
| 477 font.GetStringWidth(testcases_end[i].output), | 455 testcases_end[i].input, |
| 478 ELIDE_AT_END).size()); | 456 font_list, |
| 457 GetStringWidthF(testcases_end[i].output, font_list), |
| 458 ELIDE_AT_END).size()); |
| 479 EXPECT_EQ(kEllipsisStr, | 459 EXPECT_EQ(kEllipsisStr, |
| 480 ElideText(testcases_end[i].input, font, ellipsis_width, | 460 ElideText(testcases_end[i].input, font_list, ellipsis_width, |
| 481 ELIDE_AT_END)); | 461 ELIDE_AT_END)); |
| 482 } | 462 } |
| 483 | 463 |
| 484 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; | 464 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; |
| 485 string16 long_string_middle(data_scheme + | 465 string16 long_string_middle(data_scheme + |
| 486 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + | 466 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + |
| 487 string16(number_of_trailing_as, 'a')); | 467 string16(number_of_trailing_as, 'a')); |
| 488 UTF16Testcase testcases_middle[] = { | 468 UTF16Testcase testcases_middle[] = { |
| 489 {data_scheme + ten_a, data_scheme + ten_a}, | 469 {data_scheme + ten_a, data_scheme + ten_a}, |
| 490 {data_scheme + hundred_a, data_scheme + hundred_a}, | 470 {data_scheme + hundred_a, data_scheme + hundred_a}, |
| 491 {data_scheme + thousand_a, long_string_middle}, | 471 {data_scheme + thousand_a, long_string_middle}, |
| 492 {data_scheme + ten_thousand_a, long_string_middle}, | 472 {data_scheme + ten_thousand_a, long_string_middle}, |
| 493 {data_scheme + hundred_thousand_a, long_string_middle}, | 473 {data_scheme + hundred_thousand_a, long_string_middle}, |
| 494 {data_scheme + million_a, long_string_middle}, | 474 {data_scheme + million_a, long_string_middle}, |
| 495 }; | 475 }; |
| 496 | 476 |
| 497 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { | 477 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { |
| 498 // Compare sizes rather than actual contents because if the test fails, | 478 // Compare sizes rather than actual contents because if the test fails, |
| 499 // output is rather long. | 479 // output is rather long. |
| 500 EXPECT_EQ(testcases_middle[i].output.size(), | 480 EXPECT_EQ(testcases_middle[i].output.size(), |
| 501 ElideText(testcases_middle[i].input, font, | 481 ElideText( |
| 502 font.GetStringWidth(testcases_middle[i].output), | 482 testcases_middle[i].input, |
| 503 ELIDE_AT_END).size()); | 483 font_list, |
| 484 GetStringWidthF(testcases_middle[i].output, font_list), |
| 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_list, 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()); |
| 513 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); | 495 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); |
| 514 } | 496 } |
| 515 | 497 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 555 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 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 FontList font_list; |
| 584 const int line_height = font.GetHeight(); | 566 const int line_height = font_list.GetHeight(); |
| 585 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 567 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 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 int 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" }, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 615 { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" }, | 597 { "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" }, | 598 { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" }, |
| 617 { "Te ", test_width, line_height, false, "Te" }, | 599 { "Te ", test_width, line_height, false, "Te" }, |
| 618 { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" }, | 600 { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" }, |
| 619 }; | 601 }; |
| 620 | 602 |
| 621 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 603 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 622 std::vector<string16> lines; | 604 std::vector<string16> lines; |
| 623 EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0, | 605 EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0, |
| 624 ElideRectangleText(UTF8ToUTF16(cases[i].input), | 606 ElideRectangleText(UTF8ToUTF16(cases[i].input), |
| 625 font, | 607 font_list, |
| 626 cases[i].available_pixel_width, | 608 cases[i].available_pixel_width, |
| 627 cases[i].available_pixel_height, | 609 cases[i].available_pixel_height, |
| 628 TRUNCATE_LONG_WORDS, | 610 TRUNCATE_LONG_WORDS, |
| 629 &lines)); | 611 &lines)); |
| 630 if (cases[i].output) { | 612 if (cases[i].output) { |
| 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 FontList font_list; |
| 641 const int line_height = font.GetHeight(); | 623 const int line_height = font_list.GetHeight(); |
| 642 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 624 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 643 const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); | 625 const float test_t_width = GetStringWidthF(ASCIIToUTF16("Test T"), font_list); |
| 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 int 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 |
| 659 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 641 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 660 std::vector<string16> lines; | 642 std::vector<string16> lines; |
| 661 const WordWrapBehavior wrap_behavior = | 643 const WordWrapBehavior wrap_behavior = |
| 662 (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS); | 644 (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS); |
| 663 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, | 645 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, |
| 664 ElideRectangleText(UTF8ToUTF16(cases[i].input), | 646 ElideRectangleText(UTF8ToUTF16(cases[i].input), |
| 665 font, | 647 font_list, |
| 666 cases[i].available_pixel_width, | 648 cases[i].available_pixel_width, |
| 667 cases[i].available_pixel_height, | 649 cases[i].available_pixel_height, |
| 668 wrap_behavior, | 650 wrap_behavior, |
| 669 &lines)); | 651 &lines)); |
| 670 if (cases[i].output) { | 652 if (cases[i].output) { |
| 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 FontList font_list; |
| 681 const int kAvailableHeight = 1000; | 663 const int 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 = GetStringWidthF(kElidedTesting, font_list); |
| 684 const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); | 666 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 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 17 matching lines...) Expand all Loading... |
| 716 { "Test Tests ", test_width, WRAP_LONG_WORDS, false, "Test|Test|s" }, | 698 { "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" }, | 699 { "Test Tests T", test_width, WRAP_LONG_WORDS, false, "Test|Test|s T" }, |
| 718 { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" }, | 700 { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" }, |
| 719 { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" }, | 701 { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" }, |
| 720 }; | 702 }; |
| 721 | 703 |
| 722 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 704 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 723 std::vector<string16> lines; | 705 std::vector<string16> lines; |
| 724 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, | 706 EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, |
| 725 ElideRectangleText(UTF8ToUTF16(cases[i].input), | 707 ElideRectangleText(UTF8ToUTF16(cases[i].input), |
| 726 font, | 708 font_list, |
| 727 cases[i].available_pixel_width, | 709 cases[i].available_pixel_width, |
| 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 FontList font_list; |
| 754 const int kAvailableWidth = 235; | 727 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 728 // Use a specific font to expose the line width exceeding problem. |
| 729 font_list = FontList(Font("LucidaGrande", 12)); |
| 730 #endif |
| 731 const float kAvailableWidth = 235; |
| 755 const int kAvailableHeight = 1000; | 732 const int 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_list, |
| 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(GetStringWidthF(lines[0], font_list), kAvailableWidth); |
| 766 EXPECT_LE(font.GetStringWidth(lines[1]), kAvailableWidth); | 743 EXPECT_LE(GetStringWidthF(lines[1], font_list), 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 Loading... |
| 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 |
| OLD | NEW |