| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_font.h" | |
| 6 #include "base/file_path.h" | |
| 7 #include "base/string_util.h" | |
| 8 #include "chrome/common/gfx/text_elider.h" | |
| 9 #include "googleurl/src/gurl.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 using namespace gfx; | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const wchar_t kEllipsis[] = L"\x2026"; | |
| 17 | |
| 18 struct Testcase { | |
| 19 const std::string input; | |
| 20 const std::wstring output; | |
| 21 }; | |
| 22 | |
| 23 struct FileTestcase { | |
| 24 const FilePath::StringType input; | |
| 25 const std::wstring output; | |
| 26 }; | |
| 27 | |
| 28 struct WideTestcase { | |
| 29 const std::wstring input; | |
| 30 const std::wstring output; | |
| 31 }; | |
| 32 | |
| 33 struct TestData { | |
| 34 const std::string a; | |
| 35 const std::string b; | |
| 36 const int compare_result; | |
| 37 }; | |
| 38 | |
| 39 void RunTest(Testcase* testcases, size_t num_testcases) { | |
| 40 static const ChromeFont font; | |
| 41 for (size_t i = 0; i < num_testcases; ++i) { | |
| 42 const GURL url(testcases[i].input); | |
| 43 // Should we test with non-empty language list? | |
| 44 // That's kinda redundant with net_util_unittests. | |
| 45 EXPECT_EQ(testcases[i].output, | |
| 46 ElideUrl(url, font, font.GetStringWidth(testcases[i].output), | |
| 47 std::wstring())); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 // Test eliding of commonplace URLs. | |
| 54 TEST(TextEliderTest, TestGeneralEliding) { | |
| 55 const std::wstring kEllipsisStr(kEllipsis); | |
| 56 Testcase testcases[] = { | |
| 57 {"http://www.google.com/intl/en/ads/", | |
| 58 L"http://www.google.com/intl/en/ads/"}, | |
| 59 {"http://www.google.com/intl/en/ads/", L"www.google.com/intl/en/ads/"}, | |
| 60 // TODO(port): make this test case work on mac. | |
| 61 #if !defined(OS_MACOSX) | |
| 62 {"http://www.google.com/intl/en/ads/", | |
| 63 L"google.com/intl/" + kEllipsisStr + L"/ads/"}, | |
| 64 #endif | |
| 65 {"http://www.google.com/intl/en/ads/", | |
| 66 L"google.com/" + kEllipsisStr + L"/ads/"}, | |
| 67 {"http://www.google.com/intl/en/ads/", L"google.com/" + kEllipsisStr}, | |
| 68 {"http://www.google.com/intl/en/ads/", L"goog" + kEllipsisStr}, | |
| 69 {"https://subdomain.foo.com/bar/filename.html", | |
| 70 L"subdomain.foo.com/bar/filename.html"}, | |
| 71 {"https://subdomain.foo.com/bar/filename.html", | |
| 72 L"subdomain.foo.com/" + kEllipsisStr + L"/filename.html"}, | |
| 73 {"http://subdomain.foo.com/bar/filename.html", | |
| 74 kEllipsisStr + L"foo.com/" + kEllipsisStr + L"/filename.html"}, | |
| 75 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", | |
| 76 L"http://www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, | |
| 77 }; | |
| 78 | |
| 79 RunTest(testcases, arraysize(testcases)); | |
| 80 } | |
| 81 | |
| 82 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. | |
| 83 TEST(TextEliderTest, TestMoreEliding) { | |
| 84 const std::wstring kEllipsisStr(kEllipsis); | |
| 85 Testcase testcases[] = { | |
| 86 {"http://www.google.com/foo?bar", L"http://www.google.com/foo?bar"}, | |
| 87 {"http://xyz.google.com/foo?bar", L"xyz.google.com/foo?" + kEllipsisStr}, | |
| 88 {"http://xyz.google.com/foo?bar", L"xyz.google.com/foo" + kEllipsisStr}, | |
| 89 {"http://xyz.google.com/foo?bar", L"xyz.google.com/fo" + kEllipsisStr}, | |
| 90 {"http://a.b.com/pathname/c?d", L"a.b.com/" + kEllipsisStr + L"/c?d"}, | |
| 91 {"", L""}, | |
| 92 {"http://foo.bar..example.com...hello/test/filename.html", | |
| 93 L"foo.bar..example.com...hello/" + kEllipsisStr + L"/filename.html"}, | |
| 94 {"http://foo.bar../", L"http://foo.bar../"}, | |
| 95 {"http://xn--1lq90i.cn/foo", L"http://\x5317\x4eac.cn/foo"}, | |
| 96 {"http://me:mypass@secrethost.com:99/foo?bar#baz", | |
| 97 L"http://secrethost.com:99/foo?bar#baz"}, | |
| 98 {"http://me:mypass@ss%xxfdsf.com/foo", L"http://ss%25xxfdsf.com/foo"}, | |
| 99 {"mailto:elgoato@elgoato.com", L"mailto:elgoato@elgoato.com"}, | |
| 100 {"javascript:click(0)", L"javascript:click(0)"}, | |
| 101 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 102 L"chess.eecs.berkeley.edu:4430/login/arbitfilename"}, | |
| 103 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", | |
| 104 kEllipsisStr + L"berkeley.edu:4430/" + kEllipsisStr + L"/arbitfilename"}, | |
| 105 | |
| 106 // Unescaping. | |
| 107 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 108 L"http://www/\x4f60\x597d?q=\x4f60\x597d#\x4f60"}, | |
| 109 | |
| 110 // Invalid unescaping for path. The ref will always be valid UTF-8. We don't | |
| 111 // bother to do too many edge cases, since these are handled by the escaper | |
| 112 // unittest. | |
| 113 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", | |
| 114 L"http://www/%E4%A0%E5%A5%BD?q=\x4f60\x597d#\x4f60"}, | |
| 115 }; | |
| 116 | |
| 117 RunTest(testcases, arraysize(testcases)); | |
| 118 } | |
| 119 | |
| 120 // Test eliding of file: URLs. | |
| 121 TEST(TextEliderTest, TestFileURLEliding) { | |
| 122 const std::wstring kEllipsisStr(kEllipsis); | |
| 123 Testcase testcases[] = { | |
| 124 {"file:///C:/path1/path2/path3/filename", | |
| 125 L"file:///C:/path1/path2/path3/filename"}, | |
| 126 {"file:///C:/path1/path2/path3/filename", | |
| 127 L"C:/path1/path2/path3/filename"}, | |
| 128 // GURL parses "file:///C:path" differently on windows than it does on posix. | |
| 129 #if defined(OS_WIN) | |
| 130 {"file:///C:path1/path2/path3/filename", | |
| 131 L"C:/path1/path2/" + kEllipsisStr + L"/filename"}, | |
| 132 {"file:///C:path1/path2/path3/filename", | |
| 133 L"C:/path1/" + kEllipsisStr + L"/filename"}, | |
| 134 {"file:///C:path1/path2/path3/filename", | |
| 135 L"C:/" + kEllipsisStr + L"/filename"}, | |
| 136 #endif | |
| 137 {"file://filer/foo/bar/file", L"filer/foo/bar/file"}, | |
| 138 {"file://filer/foo/bar/file", L"filer/foo/" + kEllipsisStr + L"/file"}, | |
| 139 {"file://filer/foo/bar/file", L"filer/" + kEllipsisStr + L"/file"}, | |
| 140 }; | |
| 141 | |
| 142 RunTest(testcases, arraysize(testcases)); | |
| 143 } | |
| 144 | |
| 145 TEST(TextEliderTest, TestFilenameEliding) { | |
| 146 const std::wstring kEllipsisStr(kEllipsis); | |
| 147 const FilePath::StringType kPathSeparator = | |
| 148 FilePath::StringType().append(1, FilePath::kSeparators[0]); | |
| 149 | |
| 150 FileTestcase testcases[] = { | |
| 151 {FILE_PATH_LITERAL(""), L""}, | |
| 152 {FILE_PATH_LITERAL("."), L"."}, | |
| 153 {FILE_PATH_LITERAL("filename.exe"), L"filename.exe"}, | |
| 154 {FILE_PATH_LITERAL(".longext"), L".longext"}, | |
| 155 {FILE_PATH_LITERAL("pie"), L"pie"}, | |
| 156 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + | |
| 157 kPathSeparator + FILE_PATH_LITERAL("filename.pie"), | |
| 158 L"filename.pie"}, | |
| 159 {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + | |
| 160 kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"), | |
| 161 L"long" + kEllipsisStr + L".pie"}, | |
| 162 {FILE_PATH_LITERAL("http://path.com/filename.pie"), L"filename.pie"}, | |
| 163 {FILE_PATH_LITERAL("http://path.com/longfilename.pie"), | |
| 164 L"long" + kEllipsisStr + L".pie"}, | |
| 165 {FILE_PATH_LITERAL("piesmashingtacularpants"), L"pie" + kEllipsisStr}, | |
| 166 {FILE_PATH_LITERAL(".piesmashingtacularpants"), L".pie" + kEllipsisStr}, | |
| 167 {FILE_PATH_LITERAL("cheese."), L"cheese."}, | |
| 168 {FILE_PATH_LITERAL("file name.longext"), | |
| 169 L"file" + kEllipsisStr + L".longext"}, | |
| 170 {FILE_PATH_LITERAL("fil ename.longext"), | |
| 171 L"fil " + kEllipsisStr + L".longext"}, | |
| 172 {FILE_PATH_LITERAL("filename.longext"), | |
| 173 L"file" + kEllipsisStr + L".longext"}, | |
| 174 {FILE_PATH_LITERAL("filename.middleext.longext"), | |
| 175 L"filename.mid" + kEllipsisStr + L".longext"} | |
| 176 }; | |
| 177 | |
| 178 static const ChromeFont font; | |
| 179 for (size_t i = 0; i < arraysize(testcases); ++i) { | |
| 180 FilePath filepath(testcases[i].input); | |
| 181 EXPECT_EQ(testcases[i].output, ElideFilename(filepath, | |
| 182 font, | |
| 183 font.GetStringWidth(testcases[i].output))); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 TEST(TextEliderTest, ElideTextLongStrings) { | |
| 188 const std::wstring kEllipsisStr(kEllipsis); | |
| 189 std::wstring data_scheme(L"data:text/plain,"); | |
| 190 | |
| 191 std::wstring ten_a(10, L'a'); | |
| 192 std::wstring hundred_a(100, L'a'); | |
| 193 std::wstring thousand_a(1000, L'a'); | |
| 194 std::wstring ten_thousand_a(10000, L'a'); | |
| 195 std::wstring hundred_thousand_a(100000, L'a'); | |
| 196 std::wstring million_a(1000000, L'a'); | |
| 197 | |
| 198 WideTestcase testcases[] = { | |
| 199 {data_scheme + ten_a, | |
| 200 data_scheme + ten_a}, | |
| 201 {data_scheme + hundred_a, | |
| 202 data_scheme + hundred_a}, | |
| 203 {data_scheme + thousand_a, | |
| 204 data_scheme + std::wstring(156, L'a') + kEllipsisStr}, | |
| 205 {data_scheme + ten_thousand_a, | |
| 206 data_scheme + std::wstring(156, L'a') + kEllipsisStr}, | |
| 207 {data_scheme + hundred_thousand_a, | |
| 208 data_scheme + std::wstring(156, L'a') + kEllipsisStr}, | |
| 209 {data_scheme + million_a, | |
| 210 data_scheme + std::wstring(156, L'a') + kEllipsisStr}, | |
| 211 }; | |
| 212 | |
| 213 const ChromeFont font; | |
| 214 int ellipsis_width = font.GetStringWidth(kEllipsisStr); | |
| 215 for (size_t i = 0; i < arraysize(testcases); ++i) { | |
| 216 // Compare sizes rather than actual contents because if the test fails, | |
| 217 // output is rather long. | |
| 218 EXPECT_EQ(testcases[i].output.size(), | |
| 219 ElideText(testcases[i].input, font, | |
| 220 font.GetStringWidth(testcases[i].output)).size()); | |
| 221 EXPECT_EQ(kEllipsisStr, | |
| 222 ElideText(testcases[i].input, font, ellipsis_width)); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 // Verifies display_url is set correctly. | |
| 227 TEST(TextEliderTest, SortedDisplayURL) { | |
| 228 gfx::SortedDisplayURL d_url(GURL("http://www.google.com/"), std::wstring()); | |
| 229 EXPECT_EQ("http://www.google.com/", UTF16ToASCII(d_url.display_url())); | |
| 230 } | |
| 231 | |
| 232 // Verifies DisplayURL::Compare works correctly. | |
| 233 TEST(TextEliderTest, SortedDisplayURLCompare) { | |
| 234 UErrorCode create_status = U_ZERO_ERROR; | |
| 235 scoped_ptr<Collator> collator(Collator::createInstance(create_status)); | |
| 236 if (!U_SUCCESS(create_status)) | |
| 237 return; | |
| 238 | |
| 239 TestData tests[] = { | |
| 240 // IDN comparison. Hosts equal, so compares on path. | |
| 241 { "http://xn--1lq90i.cn/a", "http://xn--1lq90i.cn/b", -1}, | |
| 242 | |
| 243 // Because the host and after host match, this compares the full url. | |
| 244 { "http://www.x/b", "http://x/b", -1 }, | |
| 245 | |
| 246 // Because the host and after host match, this compares the full url. | |
| 247 { "http://www.a:1/b", "http://a:1/b", 1 }, | |
| 248 | |
| 249 // The hosts match, so these end up comparing on the after host portion. | |
| 250 { "http://www.x:0/b", "http://x:1/b", -1 }, | |
| 251 { "http://www.x/a", "http://x/b", -1 }, | |
| 252 { "http://x/b", "http://www.x/a", 1 }, | |
| 253 | |
| 254 // Trivial Equality. | |
| 255 { "http://a/", "http://a/", 0 }, | |
| 256 | |
| 257 // Compares just hosts. | |
| 258 { "http://www.a/", "http://b/", -1 }, | |
| 259 }; | |
| 260 | |
| 261 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 262 gfx::SortedDisplayURL url1(GURL(tests[i].a), std::wstring()); | |
| 263 gfx::SortedDisplayURL url2(GURL(tests[i].b), std::wstring()); | |
| 264 EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get())); | |
| 265 EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); | |
| 266 } | |
| 267 } | |
| OLD | NEW |