| 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" | 16 #include "ui/gfx/font_list.h" |
| 17 #include "ui/gfx/text_utils.h" | 17 #include "ui/gfx/text_utils.h" |
| 18 #include "url/gurl.h" | |
| 19 | 18 |
| 20 using base::ASCIIToUTF16; | 19 using base::ASCIIToUTF16; |
| 21 using base::UTF16ToUTF8; | 20 using base::UTF16ToUTF8; |
| 22 using base::UTF16ToWide; | 21 using base::UTF16ToWide; |
| 23 using base::UTF8ToUTF16; | 22 using base::UTF8ToUTF16; |
| 24 using base::WideToUTF16; | 23 using base::WideToUTF16; |
| 25 | 24 |
| 26 namespace gfx { | 25 namespace gfx { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 const base::string16 input; | 40 const base::string16 input; |
| 42 const base::string16 output; | 41 const base::string16 output; |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 struct TestData { | 44 struct TestData { |
| 46 const std::string a; | 45 const std::string a; |
| 47 const std::string b; | 46 const std::string b; |
| 48 const int compare_result; | 47 const int compare_result; |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 void RunUrlTest(Testcase* testcases, size_t num_testcases) { | |
| 52 static const FontList font_list; | |
| 53 for (size_t i = 0; i < num_testcases; ++i) { | |
| 54 const GURL url(testcases[i].input); | |
| 55 // Should we test with non-empty language list? | |
| 56 // That's kinda redundant with net_util_unittests. | |
| 57 const float available_width = | |
| 58 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); | |
| 59 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), | |
| 60 ElideUrl(url, font_list, available_width, std::string())); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 } // namespace | 50 } // namespace |
| 65 | 51 |
| 66 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF | 52 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 67 // that calls [NSString sizeWithFont] returns the rounded string width. | 53 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 68 #if defined(OS_IOS) | 54 // TODO(338784): Enable this on android. |
| 55 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 69 #define MAYBE_ElideEmail DISABLED_ElideEmail | 56 #define MAYBE_ElideEmail DISABLED_ElideEmail |
| 70 #else | 57 #else |
| 71 #define MAYBE_ElideEmail ElideEmail | 58 #define MAYBE_ElideEmail ElideEmail |
| 72 #endif | 59 #endif |
| 73 TEST(TextEliderTest, MAYBE_ElideEmail) { | 60 TEST(TextEliderTest, MAYBE_ElideEmail) { |
| 74 const std::string kEllipsisStr(kEllipsis); | 61 const std::string kEllipsisStr(kEllipsis); |
| 75 | 62 |
| 76 // Test emails and their expected elided forms (from which the available | 63 // Test emails and their expected elided forms (from which the available |
| 77 // widths will be derived). | 64 // widths will be derived). |
| 78 // For elided forms in which both the username and domain must be elided: | 65 // For elided forms in which both the username and domain must be elided: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 for (size_t i = 0; i < arraysize(testcases); ++i) { | 106 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 120 const base::string16 expected_output = UTF8ToUTF16(testcases[i].output); | 107 const base::string16 expected_output = UTF8ToUTF16(testcases[i].output); |
| 121 EXPECT_EQ(expected_output, | 108 EXPECT_EQ(expected_output, |
| 122 ElideEmail( | 109 ElideEmail( |
| 123 UTF8ToUTF16(testcases[i].input), | 110 UTF8ToUTF16(testcases[i].input), |
| 124 font_list, | 111 font_list, |
| 125 GetStringWidthF(expected_output, font_list))); | 112 GetStringWidthF(expected_output, font_list))); |
| 126 } | 113 } |
| 127 } | 114 } |
| 128 | 115 |
| 129 TEST(TextEliderTest, ElideEmailMoreSpace) { | 116 // TODO(338784): Enable this on android. |
| 117 #if defined(OS_ANDROID) |
| 118 #define MAYBE_ElideEmailMoreSpace DISABLED_ElideEmailMoreSpace |
| 119 #else |
| 120 #define MAYBE_ElideEmailMoreSpace ElideEmailMoreSpace |
| 121 #endif |
| 122 TEST(TextEliderTest, MAYBE_ElideEmailMoreSpace) { |
| 130 const int test_width_factors[] = { | 123 const int test_width_factors[] = { |
| 131 100, | 124 100, |
| 132 10000, | 125 10000, |
| 133 1000000, | 126 1000000, |
| 134 }; | 127 }; |
| 135 const std::string test_emails[] = { | 128 const std::string test_emails[] = { |
| 136 "a@c", | 129 "a@c", |
| 137 "test@email.com", | 130 "test@email.com", |
| 138 "short@verysuperdupperlongdomain.com", | 131 "short@verysuperdupperlongdomain.com", |
| 139 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", | 132 "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", |
| 140 }; | 133 }; |
| 141 | 134 |
| 142 const FontList font_list; | 135 const FontList font_list; |
| 143 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { | 136 for (size_t i = 0; i < arraysize(test_width_factors); ++i) { |
| 144 const int test_width = | 137 const int test_width = |
| 145 font_list.GetExpectedTextWidth(test_width_factors[i]); | 138 font_list.GetExpectedTextWidth(test_width_factors[i]); |
| 146 for (size_t j = 0; j < arraysize(test_emails); ++j) { | 139 for (size_t j = 0; j < arraysize(test_emails); ++j) { |
| 147 // Extra space is available: the email should not be elided. | 140 // Extra space is available: the email should not be elided. |
| 148 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); | 141 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); |
| 149 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); | 142 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); |
| 150 } | 143 } |
| 151 } | 144 } |
| 152 } | 145 } |
| 153 | 146 |
| 154 // Test eliding of commonplace URLs. | |
| 155 TEST(TextEliderTest, TestGeneralEliding) { | |
| 156 const std::string kEllipsisStr(kEllipsis); | |
| 157 Testcase testcases[] = { | |
| 158 {"http://www.google.com/intl/en/ads/", | |
| 159 "www.google.com/intl/en/ads/"}, | |
| 160 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, | |
| 161 {"http://www.google.com/intl/en/ads/", | |
| 162 "google.com/intl/" + kEllipsisStr + "/ads/"}, | |
| 163 {"http://www.google.com/intl/en/ads/", | |
| 164 "google.com/" + kEllipsisStr + "/ads/"}, | |
| 165 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, | |
| 166 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, | |
| 167 {"https://subdomain.foo.com/bar/filename.html", | |
| 168 "subdomain.foo.com/bar/filename.html"}, | |
| 169 {"https://subdomain.foo.com/bar/filename.html", | |
| 170 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, | |
| 171 {"http://subdomain.foo.com/bar/filename.html", | |
| 172 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, | |
| 173 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", | |
| 174 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, | |
| 175 }; | |
| 176 | |
| 177 RunUrlTest(testcases, arraysize(testcases)); | |
| 178 } | |
| 179 | |
| 180 // When there is very little space available, the elision code will shorten | |
| 181 // both path AND file name to an ellipsis - ".../...". To avoid this result, | |
| 182 // there is a hack in place that simply treats them as one string in this | |
| 183 // case. | |
| 184 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { | |
| 185 const std::string kEllipsisStr(kEllipsis); | |
| 186 | |
| 187 // Very little space, would cause double ellipsis. | |
| 188 FontList font_list; | |
| 189 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); | |
| 190 float available_width = GetStringWidthF( | |
| 191 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), | |
| 192 font_list); | |
| 193 | |
| 194 // Create the expected string, after elision. Depending on font size, the | |
| 195 // directory might become /dir... or /di... or/d... - it never should be | |
| 196 // shorter than that. (If it is, the font considers d... to be longer | |
| 197 // than .../... - that should never happen). | |
| 198 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), | |
| 199 font_list), | |
| 200 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); | |
| 201 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); | |
| 202 base::string16 expected = | |
| 203 ElideUrl(long_url, font_list, available_width, std::string()); | |
| 204 // Ensure that the expected result still contains part of the directory name. | |
| 205 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); | |
| 206 EXPECT_EQ(expected, | |
| 207 ElideUrl(url, font_list, available_width, std::string())); | |
| 208 | |
| 209 // More space available - elide directories, partially elide filename. | |
| 210 Testcase testcases[] = { | |
| 211 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", | |
| 212 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, | |
| 213 }; | |
| 214 RunUrlTest(testcases, arraysize(testcases)); | |
| 215 } | |
| 216 | |
| 217 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. | |
| 218 TEST(TextEliderTest, TestMoreEliding) { | |
| 219 const std::string kEllipsisStr(kEllipsis); | |
| 220 Testcase testcases[] = { | |
| 221 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, | |
| 222 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, | |
| 223 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, | |
| 224 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, | |
| 225 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, | |
| 226 {"", ""}, | |
| 227 {"http://foo.bar..example.com...hello/test/filename.html", | |
| 228 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, | |
| 229 {"http://foo.bar../", "foo.bar.."}, | |
| 230 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, | |
| 231 {"http://me:mypass@secrethost.com:99/foo?bar#baz", | |
| 232 "secrethost.com:99/foo?bar#baz"}, | |
| 233 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, | |
| 234 {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"}, | |
| 235 {"javascript:click(0)", "javascript:click(0)"}, | |
| 236 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 237 "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, | |
| 238 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 239 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, | |
| 240 | |
| 241 // Unescaping. | |
| 242 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 243 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, | |
| 244 | |
| 245 // Invalid unescaping for path. The ref will always be valid UTF-8. We don't | |
| 246 // bother to do too many edge cases, since these are handled by the escaper | |
| 247 // unittest. | |
| 248 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 249 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, | |
| 250 }; | |
| 251 | |
| 252 RunUrlTest(testcases, arraysize(testcases)); | |
| 253 } | |
| 254 | |
| 255 // Test eliding of file: URLs. | |
| 256 TEST(TextEliderTest, TestFileURLEliding) { | |
| 257 const std::string kEllipsisStr(kEllipsis); | |
| 258 Testcase testcases[] = { | |
| 259 {"file:///C:/path1/path2/path3/filename", | |
| 260 "file:///C:/path1/path2/path3/filename"}, | |
| 261 {"file:///C:/path1/path2/path3/filename", | |
| 262 "C:/path1/path2/path3/filename"}, | |
| 263 // GURL parses "file:///C:path" differently on windows than it does on posix. | |
| 264 #if defined(OS_WIN) | |
| 265 {"file:///C:path1/path2/path3/filename", | |
| 266 "C:/path1/path2/" + kEllipsisStr + "/filename"}, | |
| 267 {"file:///C:path1/path2/path3/filename", | |
| 268 "C:/path1/" + kEllipsisStr + "/filename"}, | |
| 269 {"file:///C:path1/path2/path3/filename", | |
| 270 "C:/" + kEllipsisStr + "/filename"}, | |
| 271 #endif | |
| 272 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, | |
| 273 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, | |
| 274 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, | |
| 275 }; | |
| 276 | |
| 277 RunUrlTest(testcases, arraysize(testcases)); | |
| 278 } | |
| 279 | |
| 280 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF | 147 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF |
| 281 // that calls [NSString sizeWithFont] returns the rounded string width. | 148 // that calls [NSString sizeWithFont] returns the rounded string width. |
| 282 #if defined(OS_IOS) | 149 // TODO(338784): Enable this on android. |
| 150 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 283 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding | 151 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding |
| 284 #else | 152 #else |
| 285 #define MAYBE_TestFilenameEliding TestFilenameEliding | 153 #define MAYBE_TestFilenameEliding TestFilenameEliding |
| 286 #endif | 154 #endif |
| 287 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { | 155 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { |
| 288 const std::string kEllipsisStr(kEllipsis); | 156 const std::string kEllipsisStr(kEllipsis); |
| 289 const base::FilePath::StringType kPathSeparator = | 157 const base::FilePath::StringType kPathSeparator = |
| 290 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); | 158 base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); |
| 291 | 159 |
| 292 FileTestcase testcases[] = { | 160 FileTestcase testcases[] = { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 static const FontList font_list; | 194 static const FontList font_list; |
| 327 for (size_t i = 0; i < arraysize(testcases); ++i) { | 195 for (size_t i = 0; i < arraysize(testcases); ++i) { |
| 328 base::FilePath filepath(testcases[i].input); | 196 base::FilePath filepath(testcases[i].input); |
| 329 base::string16 expected = UTF8ToUTF16(testcases[i].output); | 197 base::string16 expected = UTF8ToUTF16(testcases[i].output); |
| 330 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); | 198 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
| 331 EXPECT_EQ(expected, ElideFilename(filepath, font_list, | 199 EXPECT_EQ(expected, ElideFilename(filepath, font_list, |
| 332 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list))); | 200 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list))); |
| 333 } | 201 } |
| 334 } | 202 } |
| 335 | 203 |
| 336 TEST(TextEliderTest, ElideTextTruncate) { | 204 // TODO(338784): Enable this on android. |
| 205 #if defined(OS_ANDROID) |
| 206 #define MAYBE_ElideTextTruncate DISABLED_ElideTextTruncate |
| 207 #else |
| 208 #define MAYBE_ElideTextTruncate ElideTextTruncate |
| 209 #endif |
| 210 TEST(TextEliderTest, MAYBE_ElideTextTruncate) { |
| 337 const FontList font_list; | 211 const FontList font_list; |
| 338 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); | 212 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 339 struct TestData { | 213 struct TestData { |
| 340 const char* input; | 214 const char* input; |
| 341 float width; | 215 float width; |
| 342 const char* output; | 216 const char* output; |
| 343 } cases[] = { | 217 } cases[] = { |
| 344 { "", 0, "" }, | 218 { "", 0, "" }, |
| 345 { "Test", 0, "" }, | 219 { "Test", 0, "" }, |
| 346 { "", kTestWidth, "" }, | 220 { "", kTestWidth, "" }, |
| 347 { "Tes", kTestWidth, "Tes" }, | 221 { "Tes", kTestWidth, "Tes" }, |
| 348 { "Test", kTestWidth, "Test" }, | 222 { "Test", kTestWidth, "Test" }, |
| 349 { "Tests", kTestWidth, "Test" }, | 223 { "Tests", kTestWidth, "Test" }, |
| 350 }; | 224 }; |
| 351 | 225 |
| 352 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 226 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 353 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, | 227 base::string16 result = ElideText(UTF8ToUTF16(cases[i].input), font_list, |
| 354 cases[i].width, TRUNCATE_AT_END); | 228 cases[i].width, TRUNCATE_AT_END); |
| 355 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); | 229 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 356 } | 230 } |
| 357 } | 231 } |
| 358 | 232 |
| 359 TEST(TextEliderTest, ElideTextEllipsis) { | 233 // TODO(338784): Enable this on android. |
| 234 #if defined(OS_ANDROID) |
| 235 #define MAYBE_ElideTextEllipsis DISABLED_ElideTextEllipsis |
| 236 #else |
| 237 #define MAYBE_ElideTextEllipsis ElideTextEllipsis |
| 238 #endif |
| 239 TEST(TextEliderTest, MAYBE_ElideTextEllipsis) { |
| 360 const FontList font_list; | 240 const FontList font_list; |
| 361 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); | 241 const float kTestWidth = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 362 const char* kEllipsis = "\xE2\x80\xA6"; | 242 const char* kEllipsis = "\xE2\x80\xA6"; |
| 363 const float kEllipsisWidth = | 243 const float kEllipsisWidth = |
| 364 GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list); | 244 GetStringWidthF(UTF8ToUTF16(kEllipsis), font_list); |
| 365 struct TestData { | 245 struct TestData { |
| 366 const char* input; | 246 const char* input; |
| 367 float width; | 247 float width; |
| 368 const char* output; | 248 const char* output; |
| 369 } cases[] = { | 249 } cases[] = { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 394 index = text.find_first_of(first_char, index + 1); | 274 index = text.find_first_of(first_char, index + 1); |
| 395 } | 275 } |
| 396 index = text.find_first_of(second_char); | 276 index = text.find_first_of(second_char); |
| 397 while (index != base::string16::npos) { | 277 while (index != base::string16::npos) { |
| 398 EXPECT_GT(index, 0U); | 278 EXPECT_GT(index, 0U); |
| 399 EXPECT_EQ(first_char, text[index - 1]); | 279 EXPECT_EQ(first_char, text[index - 1]); |
| 400 index = text.find_first_of(second_char, index + 1); | 280 index = text.find_first_of(second_char, index + 1); |
| 401 } | 281 } |
| 402 } | 282 } |
| 403 | 283 |
| 404 TEST(TextEliderTest, ElideTextSurrogatePairs) { | 284 // TODO(338784): Enable this on android. |
| 285 #if defined(OS_ANDROID) |
| 286 #define MAYBE_ElideTextSurrogatePairs DISABLED_ElideTextSurrogatePairs |
| 287 #else |
| 288 #define MAYBE_ElideTextSurrogatePairs ElideTextSurrogatePairs |
| 289 #endif |
| 290 TEST(TextEliderTest, MAYBE_ElideTextSurrogatePairs) { |
| 405 const FontList font_list; | 291 const FontList font_list; |
| 406 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as | 292 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as |
| 407 // two characters forming a surrogate pair 0x0001D11E. | 293 // two characters forming a surrogate pair 0x0001D11E. |
| 408 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; | 294 const std::string kSurrogate = "\xF0\x9D\x84\x9E"; |
| 409 const base::string16 kTestString = | 295 const base::string16 kTestString = |
| 410 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); | 296 UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); |
| 411 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); | 297 const float kTestStringWidth = GetStringWidthF(kTestString, font_list); |
| 412 const base::char16 kSurrogateFirstChar = kTestString[0]; | 298 const base::char16 kSurrogateFirstChar = kTestString[0]; |
| 413 const base::char16 kSurrogateSecondChar = kTestString[1]; | 299 const base::char16 kSurrogateSecondChar = kTestString[1]; |
| 414 base::string16 result; | 300 base::string16 result; |
| 415 | 301 |
| 416 // Elide |kTextString| to all possible widths and check that no instance of | 302 // Elide |kTextString| to all possible widths and check that no instance of |
| 417 // |kSurrogate| was split in two. | 303 // |kSurrogate| was split in two. |
| 418 for (float width = 0; width <= kTestStringWidth; width++) { | 304 for (float width = 0; width <= kTestStringWidth; width++) { |
| 419 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); | 305 result = ElideText(kTestString, font_list, width, TRUNCATE_AT_END); |
| 420 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 306 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 421 | 307 |
| 422 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); | 308 result = ElideText(kTestString, font_list, width, ELIDE_AT_END); |
| 423 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 309 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 424 | 310 |
| 425 result = ElideText(kTestString, font_list, width, ELIDE_IN_MIDDLE); | 311 result = ElideText(kTestString, font_list, width, ELIDE_IN_MIDDLE); |
| 426 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); | 312 CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); |
| 427 } | 313 } |
| 428 } | 314 } |
| 429 | 315 |
| 430 TEST(TextEliderTest, ElideTextLongStrings) { | 316 // TODO(338784): Enable this on android. |
| 317 #if defined(OS_ANDROID) |
| 318 #define MAYBE_ElideTextLongStrings DISABLED_ElideTextLongStrings |
| 319 #else |
| 320 #define MAYBE_ElideTextLongStrings ElideTextLongStrings |
| 321 #endif |
| 322 TEST(TextEliderTest, MAYBE_ElideTextLongStrings) { |
| 431 const base::string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); | 323 const base::string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); |
| 432 base::string16 data_scheme(UTF8ToUTF16("data:text/plain,")); | 324 base::string16 data_scheme(UTF8ToUTF16("data:text/plain,")); |
| 433 size_t data_scheme_length = data_scheme.length(); | 325 size_t data_scheme_length = data_scheme.length(); |
| 434 | 326 |
| 435 base::string16 ten_a(10, 'a'); | 327 base::string16 ten_a(10, 'a'); |
| 436 base::string16 hundred_a(100, 'a'); | 328 base::string16 hundred_a(100, 'a'); |
| 437 base::string16 thousand_a(1000, 'a'); | 329 base::string16 thousand_a(1000, 'a'); |
| 438 base::string16 ten_thousand_a(10000, 'a'); | 330 base::string16 ten_thousand_a(10000, 'a'); |
| 439 base::string16 hundred_thousand_a(100000, 'a'); | 331 base::string16 hundred_thousand_a(100000, 'a'); |
| 440 base::string16 million_a(1000000, 'a'); | 332 base::string16 million_a(1000000, 'a'); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 testcases_middle[i].input, | 380 testcases_middle[i].input, |
| 489 font_list, | 381 font_list, |
| 490 GetStringWidthF(testcases_middle[i].output, font_list), | 382 GetStringWidthF(testcases_middle[i].output, font_list), |
| 491 ELIDE_AT_END).size()); | 383 ELIDE_AT_END).size()); |
| 492 EXPECT_EQ(kEllipsisStr, | 384 EXPECT_EQ(kEllipsisStr, |
| 493 ElideText(testcases_middle[i].input, font_list, ellipsis_width, | 385 ElideText(testcases_middle[i].input, font_list, ellipsis_width, |
| 494 ELIDE_AT_END)); | 386 ELIDE_AT_END)); |
| 495 } | 387 } |
| 496 } | 388 } |
| 497 | 389 |
| 498 // Verifies display_url is set correctly. | |
| 499 TEST(TextEliderTest, SortedDisplayURL) { | |
| 500 SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); | |
| 501 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); | |
| 502 } | |
| 503 | |
| 504 // Verifies DisplayURL::Compare works correctly. | |
| 505 TEST(TextEliderTest, SortedDisplayURLCompare) { | |
| 506 UErrorCode create_status = U_ZERO_ERROR; | |
| 507 scoped_ptr<icu::Collator> collator( | |
| 508 icu::Collator::createInstance(create_status)); | |
| 509 if (!U_SUCCESS(create_status)) | |
| 510 return; | |
| 511 | |
| 512 TestData tests[] = { | |
| 513 // IDN comparison. Hosts equal, so compares on path. | |
| 514 { "http://xn--1lq90i.cn/a", "http://xn--1lq90i.cn/b", -1}, | |
| 515 | |
| 516 // Because the host and after host match, this compares the full url. | |
| 517 { "http://www.x/b", "http://x/b", -1 }, | |
| 518 | |
| 519 // Because the host and after host match, this compares the full url. | |
| 520 { "http://www.a:1/b", "http://a:1/b", 1 }, | |
| 521 | |
| 522 // The hosts match, so these end up comparing on the after host portion. | |
| 523 { "http://www.x:0/b", "http://x:1/b", -1 }, | |
| 524 { "http://www.x/a", "http://x/b", -1 }, | |
| 525 { "http://x/b", "http://www.x/a", 1 }, | |
| 526 | |
| 527 // Trivial Equality. | |
| 528 { "http://a/", "http://a/", 0 }, | |
| 529 | |
| 530 // Compares just hosts. | |
| 531 { "http://www.a/", "http://b/", -1 }, | |
| 532 }; | |
| 533 | |
| 534 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 535 SortedDisplayURL url1(GURL(tests[i].a), std::string()); | |
| 536 SortedDisplayURL url2(GURL(tests[i].b), std::string()); | |
| 537 EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get())); | |
| 538 EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); | |
| 539 } | |
| 540 } | |
| 541 | |
| 542 TEST(TextEliderTest, ElideString) { | 390 TEST(TextEliderTest, ElideString) { |
| 543 struct TestData { | 391 struct TestData { |
| 544 const char* input; | 392 const char* input; |
| 545 int max_len; | 393 int max_len; |
| 546 bool result; | 394 bool result; |
| 547 const char* output; | 395 const char* output; |
| 548 } cases[] = { | 396 } cases[] = { |
| 549 { "Hello", 0, true, "" }, | 397 { "Hello", 0, true, "" }, |
| 550 { "", 0, false, "" }, | 398 { "", 0, false, "" }, |
| 551 { "Hello, my name is Tom", 1, true, "H" }, | 399 { "Hello, my name is Tom", 1, true, "H" }, |
| 552 { "Hello, my name is Tom", 2, true, "He" }, | 400 { "Hello, my name is Tom", 2, true, "He" }, |
| 553 { "Hello, my name is Tom", 3, true, "H.m" }, | 401 { "Hello, my name is Tom", 3, true, "H.m" }, |
| 554 { "Hello, my name is Tom", 4, true, "H..m" }, | 402 { "Hello, my name is Tom", 4, true, "H..m" }, |
| 555 { "Hello, my name is Tom", 5, true, "H...m" }, | 403 { "Hello, my name is Tom", 5, true, "H...m" }, |
| 556 { "Hello, my name is Tom", 6, true, "He...m" }, | 404 { "Hello, my name is Tom", 6, true, "He...m" }, |
| 557 { "Hello, my name is Tom", 7, true, "He...om" }, | 405 { "Hello, my name is Tom", 7, true, "He...om" }, |
| 558 { "Hello, my name is Tom", 10, true, "Hell...Tom" }, | 406 { "Hello, my name is Tom", 10, true, "Hell...Tom" }, |
| 559 { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" } | 407 { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" } |
| 560 }; | 408 }; |
| 561 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 409 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 562 base::string16 output; | 410 base::string16 output; |
| 563 EXPECT_EQ(cases[i].result, | 411 EXPECT_EQ(cases[i].result, |
| 564 ElideString(UTF8ToUTF16(cases[i].input), | 412 ElideString(UTF8ToUTF16(cases[i].input), |
| 565 cases[i].max_len, &output)); | 413 cases[i].max_len, &output)); |
| 566 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); | 414 EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); |
| 567 } | 415 } |
| 568 } | 416 } |
| 569 | 417 |
| 570 TEST(TextEliderTest, ElideRectangleText) { | 418 // TODO(338784): Enable this on android. |
| 419 #if defined(OS_ANDROID) |
| 420 #define MAYBE_ElideRectangleText DISABLED_ElideRectangleText |
| 421 #else |
| 422 #define MAYBE_ElideRectangleText ElideRectangleText |
| 423 #endif |
| 424 TEST(TextEliderTest, MAYBE_ElideRectangleText) { |
| 571 const FontList font_list; | 425 const FontList font_list; |
| 572 const int line_height = font_list.GetHeight(); | 426 const int line_height = font_list.GetHeight(); |
| 573 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); | 427 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 574 | 428 |
| 575 struct TestData { | 429 struct TestData { |
| 576 const char* input; | 430 const char* input; |
| 577 float available_pixel_width; | 431 float available_pixel_width; |
| 578 int available_pixel_height; | 432 int available_pixel_height; |
| 579 bool truncated_y; | 433 bool truncated_y; |
| 580 const char* output; | 434 const char* output; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 &lines)); | 471 &lines)); |
| 618 if (cases[i].output) { | 472 if (cases[i].output) { |
| 619 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 473 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 620 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; | 474 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
| 621 } else { | 475 } else { |
| 622 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; | 476 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
| 623 } | 477 } |
| 624 } | 478 } |
| 625 } | 479 } |
| 626 | 480 |
| 627 TEST(TextEliderTest, ElideRectangleTextPunctuation) { | 481 // TODO(338784): Enable this on android. |
| 482 #if defined(OS_ANDROID) |
| 483 #define MAYBE_ElideRectangleTextPunctuation \ |
| 484 DISABLED_ElideRectangleTextPunctuation |
| 485 #else |
| 486 #define MAYBE_ElideRectangleTextPunctuation ElideRectangleTextPunctuation |
| 487 #endif |
| 488 TEST(TextEliderTest, MAYBE_ElideRectangleTextPunctuation) { |
| 628 const FontList font_list; | 489 const FontList font_list; |
| 629 const int line_height = font_list.GetHeight(); | 490 const int line_height = font_list.GetHeight(); |
| 630 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); | 491 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 631 const float test_t_width = GetStringWidthF(ASCIIToUTF16("Test T"), font_list); | 492 const float test_t_width = GetStringWidthF(ASCIIToUTF16("Test T"), font_list); |
| 632 | 493 |
| 633 struct TestData { | 494 struct TestData { |
| 634 const char* input; | 495 const char* input; |
| 635 float available_pixel_width; | 496 float available_pixel_width; |
| 636 int available_pixel_height; | 497 int available_pixel_height; |
| 637 bool wrap_words; | 498 bool wrap_words; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 657 &lines)); | 518 &lines)); |
| 658 if (cases[i].output) { | 519 if (cases[i].output) { |
| 659 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 520 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 660 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; | 521 EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; |
| 661 } else { | 522 } else { |
| 662 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; | 523 EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; |
| 663 } | 524 } |
| 664 } | 525 } |
| 665 } | 526 } |
| 666 | 527 |
| 667 TEST(TextEliderTest, ElideRectangleTextLongWords) { | 528 // TODO(338784): Enable this on android. |
| 529 #if defined(OS_ANDROID) |
| 530 #define MAYBE_ElideRectangleTextLongWords DISABLED_ElideRectangleTextLongWords |
| 531 #else |
| 532 #define MAYBE_ElideRectangleTextLongWords ElideRectangleTextLongWords |
| 533 #endif |
| 534 TEST(TextEliderTest, MAYBE_ElideRectangleTextLongWords) { |
| 668 const FontList font_list; | 535 const FontList font_list; |
| 669 const int kAvailableHeight = 1000; | 536 const int kAvailableHeight = 1000; |
| 670 const base::string16 kElidedTesting = | 537 const base::string16 kElidedTesting = |
| 671 UTF8ToUTF16(std::string("Tes") + kEllipsis); | 538 UTF8ToUTF16(std::string("Tes") + kEllipsis); |
| 672 const float elided_width = GetStringWidthF(kElidedTesting, font_list); | 539 const float elided_width = GetStringWidthF(kElidedTesting, font_list); |
| 673 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); | 540 const float test_width = GetStringWidthF(ASCIIToUTF16("Test"), font_list); |
| 674 | 541 |
| 675 struct TestData { | 542 struct TestData { |
| 676 const char* input; | 543 const char* input; |
| 677 float available_pixel_width; | 544 float available_pixel_width; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); | 589 const std::string result = UTF16ToUTF8(JoinString(lines, '|')); |
| 723 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; | 590 EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; |
| 724 } | 591 } |
| 725 } | 592 } |
| 726 | 593 |
| 727 // This test is to make sure that the width of each wrapped line does not | 594 // This test is to make sure that the width of each wrapped line does not |
| 728 // exceed the available width. On some platform like Mac, this test used to | 595 // exceed the available width. On some platform like Mac, this test used to |
| 729 // fail because the truncated integer width is returned for the string | 596 // fail because the truncated integer width is returned for the string |
| 730 // and the accumulation of the truncated values causes the elide function | 597 // and the accumulation of the truncated values causes the elide function |
| 731 // to wrap incorrectly. | 598 // to wrap incorrectly. |
| 732 TEST(TextEliderTest, ElideRectangleTextCheckLineWidth) { | 599 // TODO(338784): Enable this on android. |
| 600 #if defined(OS_ANDROID) |
| 601 #define MAYBE_ElideRectangleTextCheckLineWidth \ |
| 602 DISABLED_ElideRectangleTextCheckLineWidth |
| 603 #else |
| 604 #define MAYBE_ElideRectangleTextCheckLineWidth ElideRectangleTextCheckLineWidth |
| 605 #endif |
| 606 TEST(TextEliderTest, MAYBE_ElideRectangleTextCheckLineWidth) { |
| 733 FontList font_list; | 607 FontList font_list; |
| 734 #if defined(OS_MACOSX) && !defined(OS_IOS) | 608 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 735 // Use a specific font to expose the line width exceeding problem. | 609 // Use a specific font to expose the line width exceeding problem. |
| 736 font_list = FontList(Font("LucidaGrande", 12)); | 610 font_list = FontList(Font("LucidaGrande", 12)); |
| 737 #endif | 611 #endif |
| 738 const float kAvailableWidth = 235; | 612 const float kAvailableWidth = 235; |
| 739 const int kAvailableHeight = 1000; | 613 const int kAvailableHeight = 1000; |
| 740 const char text[] = "that Russian place we used to go to after fencing"; | 614 const char text[] = "that Russian place we used to go to after fencing"; |
| 741 std::vector<base::string16> lines; | 615 std::vector<base::string16> lines; |
| 742 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), | 616 EXPECT_EQ(0, ElideRectangleText(UTF8ToUTF16(text), |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 | 842 |
| 969 // Test adds ... at right spot when there is not enough room to break at a | 843 // Test adds ... at right spot when there is not enough room to break at a |
| 970 // word boundary. | 844 // word boundary. |
| 971 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); | 845 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); |
| 972 | 846 |
| 973 // Test completely truncates string if break is on initial whitespace. | 847 // Test completely truncates string if break is on initial whitespace. |
| 974 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); | 848 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); |
| 975 } | 849 } |
| 976 | 850 |
| 977 } // namespace gfx | 851 } // namespace gfx |
| OLD | NEW |