OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/elide_url.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/font_list.h" |
| 10 #include "ui/gfx/text_elider.h" |
| 11 #include "ui/gfx/text_utils.h" |
| 12 |
| 13 using base::UTF8ToUTF16; |
| 14 using gfx::GetStringWidthF; |
| 15 using gfx::kEllipsis; |
| 16 |
| 17 namespace { |
| 18 |
| 19 struct Testcase { |
| 20 const std::string input; |
| 21 const std::string output; |
| 22 }; |
| 23 |
| 24 void RunUrlTest(Testcase* testcases, size_t num_testcases) { |
| 25 static const gfx::FontList font_list; |
| 26 for (size_t i = 0; i < num_testcases; ++i) { |
| 27 const GURL url(testcases[i].input); |
| 28 // Should we test with non-empty language list? |
| 29 // That's kinda redundant with net_util_unittests. |
| 30 const float available_width = |
| 31 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); |
| 32 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), |
| 33 ElideUrl(url, font_list, available_width, std::string())); |
| 34 } |
| 35 } |
| 36 |
| 37 // Test eliding of commonplace URLs. |
| 38 TEST(TextEliderTest, TestGeneralEliding) { |
| 39 const std::string kEllipsisStr(kEllipsis); |
| 40 Testcase testcases[] = { |
| 41 {"http://www.google.com/intl/en/ads/", |
| 42 "www.google.com/intl/en/ads/"}, |
| 43 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
| 44 {"http://www.google.com/intl/en/ads/", |
| 45 "google.com/intl/" + kEllipsisStr + "/ads/"}, |
| 46 {"http://www.google.com/intl/en/ads/", |
| 47 "google.com/" + kEllipsisStr + "/ads/"}, |
| 48 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, |
| 49 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, |
| 50 {"https://subdomain.foo.com/bar/filename.html", |
| 51 "subdomain.foo.com/bar/filename.html"}, |
| 52 {"https://subdomain.foo.com/bar/filename.html", |
| 53 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, |
| 54 {"http://subdomain.foo.com/bar/filename.html", |
| 55 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, |
| 56 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", |
| 57 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, |
| 58 }; |
| 59 |
| 60 RunUrlTest(testcases, arraysize(testcases)); |
| 61 } |
| 62 |
| 63 // When there is very little space available, the elision code will shorten |
| 64 // both path AND file name to an ellipsis - ".../...". To avoid this result, |
| 65 // there is a hack in place that simply treats them as one string in this |
| 66 // case. |
| 67 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
| 68 const std::string kEllipsisStr(kEllipsis); |
| 69 |
| 70 // Very little space, would cause double ellipsis. |
| 71 gfx::FontList font_list; |
| 72 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
| 73 float available_width = GetStringWidthF( |
| 74 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), |
| 75 font_list); |
| 76 |
| 77 // Create the expected string, after elision. Depending on font size, the |
| 78 // directory might become /dir... or /di... or/d... - it never should be |
| 79 // shorter than that. (If it is, the font considers d... to be longer |
| 80 // than .../... - that should never happen). |
| 81 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), |
| 82 font_list), |
| 83 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); |
| 84 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
| 85 base::string16 expected = |
| 86 ElideUrl(long_url, font_list, available_width, std::string()); |
| 87 // Ensure that the expected result still contains part of the directory name. |
| 88 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
| 89 EXPECT_EQ(expected, |
| 90 ElideUrl(url, font_list, available_width, std::string())); |
| 91 |
| 92 // More space available - elide directories, partially elide filename. |
| 93 Testcase testcases[] = { |
| 94 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", |
| 95 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, |
| 96 }; |
| 97 RunUrlTest(testcases, arraysize(testcases)); |
| 98 } |
| 99 |
| 100 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. |
| 101 TEST(TextEliderTest, TestMoreEliding) { |
| 102 const std::string kEllipsisStr(kEllipsis); |
| 103 Testcase testcases[] = { |
| 104 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, |
| 105 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, |
| 106 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, |
| 107 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, |
| 108 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, |
| 109 {"", ""}, |
| 110 {"http://foo.bar..example.com...hello/test/filename.html", |
| 111 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, |
| 112 {"http://foo.bar../", "foo.bar.."}, |
| 113 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, |
| 114 {"http://me:mypass@secrethost.com:99/foo?bar#baz", |
| 115 "secrethost.com:99/foo?bar#baz"}, |
| 116 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, |
| 117 {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"}, |
| 118 {"javascript:click(0)", "javascript:click(0)"}, |
| 119 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 120 "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, |
| 121 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 122 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, |
| 123 |
| 124 // Unescaping. |
| 125 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", |
| 126 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, |
| 127 |
| 128 // Invalid unescaping for path. The ref will always be valid UTF-8. We don't |
| 129 // bother to do too many edge cases, since these are handled by the escaper |
| 130 // unittest. |
| 131 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", |
| 132 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, |
| 133 }; |
| 134 |
| 135 RunUrlTest(testcases, arraysize(testcases)); |
| 136 } |
| 137 |
| 138 // Test eliding of file: URLs. |
| 139 TEST(TextEliderTest, TestFileURLEliding) { |
| 140 const std::string kEllipsisStr(kEllipsis); |
| 141 Testcase testcases[] = { |
| 142 {"file:///C:/path1/path2/path3/filename", |
| 143 "file:///C:/path1/path2/path3/filename"}, |
| 144 {"file:///C:/path1/path2/path3/filename", |
| 145 "C:/path1/path2/path3/filename"}, |
| 146 // GURL parses "file:///C:path" differently on windows than it does on posix. |
| 147 #if defined(OS_WIN) |
| 148 {"file:///C:path1/path2/path3/filename", |
| 149 "C:/path1/path2/" + kEllipsisStr + "/filename"}, |
| 150 {"file:///C:path1/path2/path3/filename", |
| 151 "C:/path1/" + kEllipsisStr + "/filename"}, |
| 152 {"file:///C:path1/path2/path3/filename", |
| 153 "C:/" + kEllipsisStr + "/filename"}, |
| 154 #endif |
| 155 {"file://filer/foo/bar/file", "filer/foo/bar/file"}, |
| 156 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, |
| 157 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, |
| 158 }; |
| 159 |
| 160 RunUrlTest(testcases, arraysize(testcases)); |
| 161 } |
| 162 |
| 163 } // namespace |
OLD | NEW |