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

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

Issue 143463006: Remove net dependency from ui/gfx (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: also drop url deps Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Unit tests for eliding and formatting utility functions. 5 // Unit tests for eliding and formatting utility functions.
6 6
7 #include "ui/gfx/text_elider.h" 7 #include "ui/gfx/text_elider.h"
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/font.h" 15 #include "ui/gfx/font.h"
16 #include "ui/gfx/font_list.h" 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
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 #if defined(OS_IOS)
69 #define MAYBE_ElideEmail DISABLED_ElideEmail 55 #define MAYBE_ElideEmail DISABLED_ElideEmail
70 #else 56 #else
71 #define MAYBE_ElideEmail ElideEmail 57 #define MAYBE_ElideEmail ElideEmail
72 #endif 58 #endif
73 TEST(TextEliderTest, MAYBE_ElideEmail) { 59 TEST(TextEliderTest, MAYBE_ElideEmail) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 const int test_width = 130 const int test_width =
145 font_list.GetExpectedTextWidth(test_width_factors[i]); 131 font_list.GetExpectedTextWidth(test_width_factors[i]);
146 for (size_t j = 0; j < arraysize(test_emails); ++j) { 132 for (size_t j = 0; j < arraysize(test_emails); ++j) {
147 // Extra space is available: the email should not be elided. 133 // Extra space is available: the email should not be elided.
148 const base::string16 test_email = UTF8ToUTF16(test_emails[j]); 134 const base::string16 test_email = UTF8ToUTF16(test_emails[j]);
149 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width)); 135 EXPECT_EQ(test_email, ElideEmail(test_email, font_list, test_width));
150 } 136 }
151 } 137 }
152 } 138 }
153 139
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 140 // TODO(ios): This test fails on iOS because iOS version of GetStringWidthF
281 // that calls [NSString sizeWithFont] returns the rounded string width. 141 // that calls [NSString sizeWithFont] returns the rounded string width.
282 #if defined(OS_IOS) 142 #if defined(OS_IOS)
283 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding 143 #define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding
284 #else 144 #else
285 #define MAYBE_TestFilenameEliding TestFilenameEliding 145 #define MAYBE_TestFilenameEliding TestFilenameEliding
286 #endif 146 #endif
287 TEST(TextEliderTest, MAYBE_TestFilenameEliding) { 147 TEST(TextEliderTest, MAYBE_TestFilenameEliding) {
288 const std::string kEllipsisStr(kEllipsis); 148 const std::string kEllipsisStr(kEllipsis);
289 const base::FilePath::StringType kPathSeparator = 149 const base::FilePath::StringType kPathSeparator =
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 testcases_middle[i].input, 348 testcases_middle[i].input,
489 font_list, 349 font_list,
490 GetStringWidthF(testcases_middle[i].output, font_list), 350 GetStringWidthF(testcases_middle[i].output, font_list),
491 ELIDE_AT_END).size()); 351 ELIDE_AT_END).size());
492 EXPECT_EQ(kEllipsisStr, 352 EXPECT_EQ(kEllipsisStr,
493 ElideText(testcases_middle[i].input, font_list, ellipsis_width, 353 ElideText(testcases_middle[i].input, font_list, ellipsis_width,
494 ELIDE_AT_END)); 354 ELIDE_AT_END));
495 } 355 }
496 } 356 }
497 357
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) { 358 TEST(TextEliderTest, ElideString) {
543 struct TestData { 359 struct TestData {
544 const char* input; 360 const char* input;
545 int max_len; 361 int max_len;
546 bool result; 362 bool result;
547 const char* output; 363 const char* output;
548 } cases[] = { 364 } cases[] = {
549 { "Hello", 0, true, "" }, 365 { "Hello", 0, true, "" },
550 { "", 0, false, "" }, 366 { "", 0, false, "" },
551 { "Hello, my name is Tom", 1, true, "H" }, 367 { "Hello, my name is Tom", 1, true, "H" },
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 784
969 // Test adds ... at right spot when there is not enough room to break at a 785 // Test adds ... at right spot when there is not enough room to break at a
970 // word boundary. 786 // word boundary.
971 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); 787 EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11)));
972 788
973 // Test completely truncates string if break is on initial whitespace. 789 // Test completely truncates string if break is on initial whitespace.
974 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); 790 EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2)));
975 } 791 }
976 792
977 } // namespace gfx 793 } // namespace gfx
OLDNEW
« chrome/browser/ui/tests/elide_url_unittest.cc ('K') | « ui/gfx/text_elider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698