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

Side by Side Diff: components/url_formatter/elide_url_unittest.cc

Issue 1171333003: Move net::FormatUrl and friends outside of //net and into //components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase again now that CQ is fixed Created 5 years, 4 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
« no previous file with comments | « components/url_formatter/elide_url.cc ('k') | components/url_formatter/url_fixer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/secure_display/elide_url.h" 5 #include "components/url_formatter/elide_url.h"
6 6
7 #include "base/ios/ios_util.h" 7 #include "base/ios/ios_util.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/text_elider.h"
12 #include "ui/gfx/text_utils.h"
13 #include "url/gurl.h" 10 #include "url/gurl.h"
14 11
15 using base::UTF8ToUTF16; 12 #if !defined(OS_ANDROID)
16 using gfx::GetStringWidthF; 13 #include "ui/gfx/font_list.h" // nogncheck
17 using gfx::kEllipsis; 14 #include "ui/gfx/text_elider.h" // nogncheck
15 #include "ui/gfx/text_utils.h" // nogncheck
16 #endif
18 17
19 namespace { 18 namespace {
20 19
21 struct Testcase { 20 struct Testcase {
22 const std::string input; 21 const std::string input;
23 const std::string output; 22 const std::string output;
24 }; 23 };
25 24
26 #if !defined(OS_ANDROID) 25 #if !defined(OS_ANDROID)
27 void RunUrlTest(Testcase* testcases, size_t num_testcases) { 26 void RunUrlTest(Testcase* testcases, size_t num_testcases) {
28 static const gfx::FontList font_list; 27 static const gfx::FontList font_list;
29 for (size_t i = 0; i < num_testcases; ++i) { 28 for (size_t i = 0; i < num_testcases; ++i) {
30 const GURL url(testcases[i].input); 29 const GURL url(testcases[i].input);
31 // Should we test with non-empty language list? 30 // Should we test with non-empty language list?
32 // That's kinda redundant with net_util_unittests. 31 // That's kinda redundant with net_util_unittests.
33 const float available_width = 32 const float available_width =
34 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); 33 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
35 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), 34 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
36 secure_display::ElideUrl(url, font_list, available_width, 35 url_formatter::ElideUrl(url, font_list, available_width,
37 std::string())); 36 std::string()));
38 } 37 }
39 } 38 }
40 39
41 // Test eliding of commonplace URLs. 40 // Test eliding of commonplace URLs.
42 TEST(TextEliderTest, TestGeneralEliding) { 41 TEST(TextEliderTest, TestGeneralEliding) {
43 const std::string kEllipsisStr(kEllipsis); 42 const std::string kEllipsisStr(gfx::kEllipsis);
44 Testcase testcases[] = { 43 Testcase testcases[] = {
45 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, 44 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
46 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, 45 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
47 {"http://www.google.com/intl/en/ads/", 46 {"http://www.google.com/intl/en/ads/",
48 "google.com/intl/" + kEllipsisStr + "/ads/"}, 47 "google.com/intl/" + kEllipsisStr + "/ads/"},
49 {"http://www.google.com/intl/en/ads/", 48 {"http://www.google.com/intl/en/ads/",
50 "google.com/" + kEllipsisStr + "/ads/"}, 49 "google.com/" + kEllipsisStr + "/ads/"},
51 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, 50 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
52 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, 51 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
53 {"https://subdomain.foo.com/bar/filename.html", 52 {"https://subdomain.foo.com/bar/filename.html",
54 "subdomain.foo.com/bar/filename.html"}, 53 "subdomain.foo.com/bar/filename.html"},
55 {"https://subdomain.foo.com/bar/filename.html", 54 {"https://subdomain.foo.com/bar/filename.html",
56 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, 55 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
57 {"http://subdomain.foo.com/bar/filename.html", 56 {"http://subdomain.foo.com/bar/filename.html",
58 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, 57 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
59 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", 58 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
60 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, 59 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
61 }; 60 };
62 61
63 RunUrlTest(testcases, arraysize(testcases)); 62 RunUrlTest(testcases, arraysize(testcases));
64 } 63 }
65 64
66 // When there is very little space available, the elision code will shorten 65 // When there is very little space available, the elision code will shorten
67 // both path AND file name to an ellipsis - ".../...". To avoid this result, 66 // both path AND file name to an ellipsis - ".../...". To avoid this result,
68 // there is a hack in place that simply treats them as one string in this 67 // there is a hack in place that simply treats them as one string in this
69 // case. 68 // case.
70 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { 69 TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
71 const std::string kEllipsisStr(kEllipsis); 70 const std::string kEllipsisStr(gfx::kEllipsis);
72 71
73 // Very little space, would cause double ellipsis. 72 // Very little space, would cause double ellipsis.
74 gfx::FontList font_list; 73 gfx::FontList font_list;
75 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); 74 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
76 float available_width = GetStringWidthF( 75 float available_width = gfx::GetStringWidthF(
77 UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), 76 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
78 font_list); 77 font_list);
79 78
80 // Create the expected string, after elision. Depending on font size, the 79 // Create the expected string, after elision. Depending on font size, the
81 // directory might become /dir... or /di... or/d... - it never should be 80 // directory might become /dir... or /di... or/d... - it never should be
82 // shorter than that. (If it is, the font considers d... to be longer 81 // shorter than that. (If it is, the font considers d... to be longer
83 // than .../... - that should never happen). 82 // than .../... - that should never happen).
84 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), 83 ASSERT_GT(
85 font_list), 84 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
86 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); 85 font_list),
86 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
87 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); 87 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
88 base::string16 expected = secure_display::ElideUrl( 88 base::string16 expected = url_formatter::ElideUrl(
89 long_url, font_list, available_width, std::string()); 89 long_url, font_list, available_width, std::string());
90 // Ensure that the expected result still contains part of the directory name. 90 // Ensure that the expected result still contains part of the directory name.
91 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); 91 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
92 EXPECT_EQ(expected, secure_display::ElideUrl(url, font_list, available_width, 92 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width,
93 std::string())); 93 std::string()));
94 94
95 // More space available - elide directories, partially elide filename. 95 // More space available - elide directories, partially elide filename.
96 Testcase testcases[] = { 96 Testcase testcases[] = {
97 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", 97 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
98 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, 98 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
99 }; 99 };
100 RunUrlTest(testcases, arraysize(testcases)); 100 RunUrlTest(testcases, arraysize(testcases));
101 } 101 }
102 102
103 // Test eliding of empty strings, URLs with ports, passwords, queries, etc. 103 // Test eliding of empty strings, URLs with ports, passwords, queries, etc.
104 TEST(TextEliderTest, TestMoreEliding) { 104 TEST(TextEliderTest, TestMoreEliding) {
105 const std::string kEllipsisStr(kEllipsis); 105 const std::string kEllipsisStr(gfx::kEllipsis);
106 Testcase testcases[] = { 106 Testcase testcases[] = {
107 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, 107 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
108 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, 108 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
109 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, 109 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
110 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, 110 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
111 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, 111 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
112 {"", ""}, 112 {"", ""},
113 {"http://foo.bar..example.com...hello/test/filename.html", 113 {"http://foo.bar..example.com...hello/test/filename.html",
114 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, 114 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
115 {"http://foo.bar../", "foo.bar.."}, 115 {"http://foo.bar../", "foo.bar.."},
(...skipping 19 matching lines...) Expand all
135 // unittest. 135 // unittest.
136 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", 136 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
137 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, 137 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
138 }; 138 };
139 139
140 RunUrlTest(testcases, arraysize(testcases)); 140 RunUrlTest(testcases, arraysize(testcases));
141 } 141 }
142 142
143 // Test eliding of file: URLs. 143 // Test eliding of file: URLs.
144 TEST(TextEliderTest, TestFileURLEliding) { 144 TEST(TextEliderTest, TestFileURLEliding) {
145 const std::string kEllipsisStr(kEllipsis); 145 const std::string kEllipsisStr(gfx::kEllipsis);
146 Testcase testcases[] = { 146 Testcase testcases[] = {
147 {"file:///C:/path1/path2/path3/filename", 147 {"file:///C:/path1/path2/path3/filename",
148 "file:///C:/path1/path2/path3/filename"}, 148 "file:///C:/path1/path2/path3/filename"},
149 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"}, 149 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
150 // GURL parses "file:///C:path" differently on windows than it does on posix. 150 // GURL parses "file:///C:path" differently on windows than it does on posix.
151 #if defined(OS_WIN) 151 #if defined(OS_WIN)
152 {"file:///C:path1/path2/path3/filename", 152 {"file:///C:path1/path2/path3/filename",
153 "C:/path1/path2/" + kEllipsisStr + "/filename"}, 153 "C:/path1/path2/" + kEllipsisStr + "/filename"},
154 {"file:///C:path1/path2/path3/filename", 154 {"file:///C:path1/path2/path3/filename",
155 "C:/path1/" + kEllipsisStr + "/filename"}, 155 "C:/path1/" + kEllipsisStr + "/filename"},
(...skipping 15 matching lines...) Expand all
171 } 171 }
172 172
173 TEST(TextEliderTest, TestHostEliding) { 173 TEST(TextEliderTest, TestHostEliding) {
174 #if defined(OS_IOS) 174 #if defined(OS_IOS)
175 // TODO(eugenebut): Disable test on iOS9 crbug.com/513703 175 // TODO(eugenebut): Disable test on iOS9 crbug.com/513703
176 if (base::ios::IsRunningOnIOS9OrLater()) { 176 if (base::ios::IsRunningOnIOS9OrLater()) {
177 LOG(WARNING) << "Test disabled on iOS9."; 177 LOG(WARNING) << "Test disabled on iOS9.";
178 return; 178 return;
179 } 179 }
180 #endif 180 #endif
181 const std::string kEllipsisStr(kEllipsis); 181 const std::string kEllipsisStr(gfx::kEllipsis);
182 Testcase testcases[] = { 182 Testcase testcases[] = {
183 {"http://google.com", "google.com"}, 183 {"http://google.com", "google.com"},
184 {"http://subdomain.google.com", kEllipsisStr + ".google.com"}, 184 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
185 {"http://reallyreallyreallylongdomainname.com", 185 {"http://reallyreallyreallylongdomainname.com",
186 "reallyreallyreallylongdomainname.com"}, 186 "reallyreallyreallylongdomainname.com"},
187 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"}, 187 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
188 {"http://foo", "foo"}, 188 {"http://foo", "foo"},
189 {"http://foo.bar", "foo.bar"}, 189 {"http://foo.bar", "foo.bar"},
190 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"}, 190 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
191 // IOS width calculations are off by a letter from other platforms for 191 // IOS width calculations are off by a letter from other platforms for
192 // some strings from other platforms, probably for strings with too 192 // some strings from other platforms, probably for strings with too
193 // many kerned letters on the default font set. 193 // many kerned letters on the default font set.
194 #if !defined(OS_IOS) 194 #if !defined(OS_IOS)
195 {"http://subdomain.reallylongdomainname.com", 195 {"http://subdomain.reallylongdomainname.com",
196 kEllipsisStr + "ain.reallylongdomainname.com"}, 196 kEllipsisStr + "ain.reallylongdomainname.com"},
197 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"}, 197 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
198 #endif // !defined(OS_IOS) 198 #endif // !defined(OS_IOS)
199 }; 199 };
200 200
201 for (size_t i = 0; i < arraysize(testcases); ++i) { 201 for (size_t i = 0; i < arraysize(testcases); ++i) {
202 const float available_width = 202 const float available_width = gfx::GetStringWidthF(
203 GetStringWidthF(UTF8ToUTF16(testcases[i].output), gfx::FontList()); 203 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
204 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), 204 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
205 secure_display::ElideHost(GURL(testcases[i].input), 205 url_formatter::ElideHost(GURL(testcases[i].input),
206 gfx::FontList(), available_width)); 206 gfx::FontList(), available_width));
207 } 207 }
208 208
209 // Trying to elide to a really short length will still keep the full TLD+1 209 // Trying to elide to a really short length will still keep the full TLD+1
210 EXPECT_EQ( 210 EXPECT_EQ(
211 base::ASCIIToUTF16("google.com"), 211 base::ASCIIToUTF16("google.com"),
212 secure_display::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); 212 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
213 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), 213 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
214 secure_display::ElideHost(GURL("http://subdomain.google.com"), 214 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
215 gfx::FontList(), 2)); 215 gfx::FontList(), 2));
216 EXPECT_EQ( 216 EXPECT_EQ(
217 base::ASCIIToUTF16("foo.bar"), 217 base::ASCIIToUTF16("foo.bar"),
218 secure_display::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); 218 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
219 } 219 }
220 220
221 #endif // !defined(OS_ANDROID) 221 #endif // !defined(OS_ANDROID)
222 222
223 TEST(TextEliderTest, FormatUrlForSecurityDisplay) { 223 TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
224 struct OriginTestData { 224 struct OriginTestData {
225 const char* const description; 225 const char* const description;
226 const char* const input; 226 const char* const input;
227 const wchar_t* const output; 227 const wchar_t* const output;
228 }; 228 };
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 {"Trailing dot in DNS name", "https://www.example.com./get/goat", 297 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
298 L"https://www.example.com."}, 298 L"https://www.example.com."},
299 {"Blob URL", 299 {"Blob URL",
300 "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", 300 "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
301 L"blob:http%3A//www.html5rocks.com/" 301 L"blob:http%3A//www.html5rocks.com/"
302 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}, 302 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"},
303 }; 303 };
304 304
305 const char languages[] = "zh-TW,en-US,en,am,ar-EG,ar"; 305 const char languages[] = "zh-TW,en-US,en,am,ar-EG,ar";
306 for (size_t i = 0; i < arraysize(tests); ++i) { 306 for (size_t i = 0; i < arraysize(tests); ++i) {
307 base::string16 formatted = secure_display::FormatUrlForSecurityDisplay( 307 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(
308 GURL(tests[i].input), std::string()); 308 GURL(tests[i].input), std::string());
309 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) 309 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
310 << tests[i].description; 310 << tests[i].description;
311 base::string16 formatted_with_languages = 311 base::string16 formatted_with_languages =
312 secure_display::FormatUrlForSecurityDisplay(GURL(tests[i].input), 312 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input),
313 languages); 313 languages);
314 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted_with_languages) 314 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted_with_languages)
315 << tests[i].description; 315 << tests[i].description;
316 } 316 }
317 317
318 base::string16 formatted = 318 base::string16 formatted =
319 secure_display::FormatUrlForSecurityDisplay(GURL(), std::string()); 319 url_formatter::FormatUrlForSecurityDisplay(GURL(), std::string());
320 EXPECT_EQ(base::string16(), formatted) 320 EXPECT_EQ(base::string16(), formatted)
321 << "Explicitly test the 0-argument GURL constructor"; 321 << "Explicitly test the 0-argument GURL constructor";
322 } 322 }
323 323
324 } // namespace 324 } // namespace
OLDNEW
« no previous file with comments | « components/url_formatter/elide_url.cc ('k') | components/url_formatter/url_fixer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698