OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/i18n/rtl.h" | 6 #include "base/i18n/rtl.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, | 75 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, |
76 }; | 76 }; |
77 | 77 |
78 RunTest(testcases, arraysize(testcases)); | 78 RunTest(testcases, arraysize(testcases)); |
79 } | 79 } |
80 | 80 |
81 // When there is very little space available, the elision code will shorten | 81 // When there is very little space available, the elision code will shorten |
82 // both path AND file name to an ellipsis - ".../...". To avoid this result, | 82 // both path AND file name to an ellipsis - ".../...". To avoid this result, |
83 // there is a hack in place that simply treats them as one string in this | 83 // there is a hack in place that simply treats them as one string in this |
84 // case. | 84 // case. |
85 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) | 85 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
86 { | |
87 const std::string kEllipsisStr(kEllipsis); | 86 const std::string kEllipsisStr(kEllipsis); |
88 | 87 |
89 // Very little space, would cause double ellipsis. | 88 // Very little space, would cause double ellipsis. |
90 gfx::Font font; | 89 gfx::Font font; |
91 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); | 90 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
92 int available_width = font.GetStringWidth( | 91 int available_width = font.GetStringWidth( |
93 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); | 92 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); |
94 EXPECT_EQ(UTF8ToUTF16("battersbox.com/dir" + kEllipsisStr), | 93 EXPECT_EQ(UTF8ToUTF16("battersbox.com/dir" + kEllipsisStr), |
95 ElideUrl(url, font, available_width, std::string())); | 94 ElideUrl(url, font, available_width, std::string())); |
96 | 95 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 for (size_t i = 0; i < arraysize(testcases); ++i) { | 207 for (size_t i = 0; i < arraysize(testcases); ++i) { |
209 FilePath filepath(testcases[i].input); | 208 FilePath filepath(testcases[i].input); |
210 string16 expected = UTF8ToUTF16(testcases[i].output); | 209 string16 expected = UTF8ToUTF16(testcases[i].output); |
211 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); | 210 expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); |
212 EXPECT_EQ(expected, ElideFilename(filepath, | 211 EXPECT_EQ(expected, ElideFilename(filepath, |
213 font, | 212 font, |
214 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)))); | 213 font.GetStringWidth(UTF8ToUTF16(testcases[i].output)))); |
215 } | 214 } |
216 } | 215 } |
217 | 216 |
| 217 TEST(TextEliderTest, ElideTextTruncate) { |
| 218 const gfx::Font font; |
| 219 const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); |
| 220 struct TestData { |
| 221 const char* input; |
| 222 int width; |
| 223 const char* output; |
| 224 } cases[] = { |
| 225 { "", 0, "" }, |
| 226 { "Test", 0, "" }, |
| 227 { "", kTestWidth, "" }, |
| 228 { "Tes", kTestWidth, "Tes" }, |
| 229 { "Test", kTestWidth, "Test" }, |
| 230 { "Tests", kTestWidth, "Test" }, |
| 231 }; |
| 232 |
| 233 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 234 string16 result = ui::ElideText(UTF8ToUTF16(cases[i].input), font, |
| 235 cases[i].width, ui::TRUNCATE_AT_END); |
| 236 EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); |
| 237 } |
| 238 } |
| 239 |
218 TEST(TextEliderTest, ElideTextLongStrings) { | 240 TEST(TextEliderTest, ElideTextLongStrings) { |
219 const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); | 241 const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); |
220 string16 data_scheme(UTF8ToUTF16("data:text/plain,")); | 242 string16 data_scheme(UTF8ToUTF16("data:text/plain,")); |
221 size_t data_scheme_length = data_scheme.length(); | 243 size_t data_scheme_length = data_scheme.length(); |
222 | 244 |
223 string16 ten_a(10, 'a'); | 245 string16 ten_a(10, 'a'); |
224 string16 hundred_a(100, 'a'); | 246 string16 hundred_a(100, 'a'); |
225 string16 thousand_a(1000, 'a'); | 247 string16 thousand_a(1000, 'a'); |
226 string16 ten_thousand_a(10000, 'a'); | 248 string16 ten_thousand_a(10000, 'a'); |
227 string16 hundred_thousand_a(100000, 'a'); | 249 string16 hundred_thousand_a(100000, 'a'); |
(...skipping 12 matching lines...) Expand all Loading... |
240 }; | 262 }; |
241 | 263 |
242 const gfx::Font font; | 264 const gfx::Font font; |
243 int ellipsis_width = font.GetStringWidth(kEllipsisStr); | 265 int ellipsis_width = font.GetStringWidth(kEllipsisStr); |
244 for (size_t i = 0; i < arraysize(testcases_end); ++i) { | 266 for (size_t i = 0; i < arraysize(testcases_end); ++i) { |
245 // Compare sizes rather than actual contents because if the test fails, | 267 // Compare sizes rather than actual contents because if the test fails, |
246 // output is rather long. | 268 // output is rather long. |
247 EXPECT_EQ(testcases_end[i].output.size(), | 269 EXPECT_EQ(testcases_end[i].output.size(), |
248 ElideText(testcases_end[i].input, font, | 270 ElideText(testcases_end[i].input, font, |
249 font.GetStringWidth(testcases_end[i].output), | 271 font.GetStringWidth(testcases_end[i].output), |
250 false).size()); | 272 ui::ELIDE_AT_END).size()); |
251 EXPECT_EQ(kEllipsisStr, | 273 EXPECT_EQ(kEllipsisStr, |
252 ElideText(testcases_end[i].input, font, ellipsis_width, false)); | 274 ElideText(testcases_end[i].input, font, ellipsis_width, |
| 275 ui::ELIDE_AT_END)); |
253 } | 276 } |
254 | 277 |
255 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; | 278 size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; |
256 string16 long_string_middle(data_scheme + | 279 string16 long_string_middle(data_scheme + |
257 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + | 280 string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + |
258 string16(number_of_trailing_as, 'a')); | 281 string16(number_of_trailing_as, 'a')); |
259 UTF16Testcase testcases_middle[] = { | 282 UTF16Testcase testcases_middle[] = { |
260 {data_scheme + ten_a, data_scheme + ten_a}, | 283 {data_scheme + ten_a, data_scheme + ten_a}, |
261 {data_scheme + hundred_a, data_scheme + hundred_a}, | 284 {data_scheme + hundred_a, data_scheme + hundred_a}, |
262 {data_scheme + thousand_a, long_string_middle}, | 285 {data_scheme + thousand_a, long_string_middle}, |
263 {data_scheme + ten_thousand_a, long_string_middle}, | 286 {data_scheme + ten_thousand_a, long_string_middle}, |
264 {data_scheme + hundred_thousand_a, long_string_middle}, | 287 {data_scheme + hundred_thousand_a, long_string_middle}, |
265 {data_scheme + million_a, long_string_middle}, | 288 {data_scheme + million_a, long_string_middle}, |
266 }; | 289 }; |
267 | 290 |
268 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { | 291 for (size_t i = 0; i < arraysize(testcases_middle); ++i) { |
269 // Compare sizes rather than actual contents because if the test fails, | 292 // Compare sizes rather than actual contents because if the test fails, |
270 // output is rather long. | 293 // output is rather long. |
271 EXPECT_EQ(testcases_middle[i].output.size(), | 294 EXPECT_EQ(testcases_middle[i].output.size(), |
272 ElideText(testcases_middle[i].input, font, | 295 ElideText(testcases_middle[i].input, font, |
273 font.GetStringWidth(testcases_middle[i].output), | 296 font.GetStringWidth(testcases_middle[i].output), |
274 false).size()); | 297 ui::ELIDE_AT_END).size()); |
275 EXPECT_EQ(kEllipsisStr, | 298 EXPECT_EQ(kEllipsisStr, |
276 ElideText(testcases_middle[i].input, font, ellipsis_width, | 299 ElideText(testcases_middle[i].input, font, ellipsis_width, |
277 false)); | 300 ui::ELIDE_AT_END)); |
278 } | 301 } |
279 } | 302 } |
280 | 303 |
281 // Verifies display_url is set correctly. | 304 // Verifies display_url is set correctly. |
282 TEST(TextEliderTest, SortedDisplayURL) { | 305 TEST(TextEliderTest, SortedDisplayURL) { |
283 ui::SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); | 306 ui::SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); |
284 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); | 307 EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); |
285 } | 308 } |
286 | 309 |
287 // Verifies DisplayURL::Compare works correctly. | 310 // Verifies DisplayURL::Compare works correctly. |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 | 591 |
569 // Test adds ... at right spot when there is not enough room to break at a | 592 // Test adds ... at right spot when there is not enough room to break at a |
570 // word boundary. | 593 // word boundary. |
571 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 11))); | 594 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(ui::TruncateString(string, 11))); |
572 | 595 |
573 // Test completely truncates string if break is on initial whitespace. | 596 // Test completely truncates string if break is on initial whitespace. |
574 EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(ASCIIToUTF16(" "), 2))); | 597 EXPECT_EQ(L"\x2026", UTF16ToWide(ui::TruncateString(ASCIIToUTF16(" "), 2))); |
575 } | 598 } |
576 | 599 |
577 } // namespace ui | 600 } // namespace ui |
OLD | NEW |