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

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: Fold in secure_display to url_formatter 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
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" 10 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/text_elider.h" 11 #include "ui/gfx/text_elider.h"
12 #include "ui/gfx/text_utils.h" 12 #include "ui/gfx/text_utils.h"
13 #include "url/gurl.h" 13 #include "url/gurl.h"
14 14
15 using base::UTF8ToUTF16; 15 using base::UTF8ToUTF16;
(...skipping 10 matching lines...) Expand all
26 #if !defined(OS_ANDROID) 26 #if !defined(OS_ANDROID)
27 void RunUrlTest(Testcase* testcases, size_t num_testcases) { 27 void RunUrlTest(Testcase* testcases, size_t num_testcases) {
28 static const gfx::FontList font_list; 28 static const gfx::FontList font_list;
29 for (size_t i = 0; i < num_testcases; ++i) { 29 for (size_t i = 0; i < num_testcases; ++i) {
30 const GURL url(testcases[i].input); 30 const GURL url(testcases[i].input);
31 // Should we test with non-empty language list? 31 // Should we test with non-empty language list?
32 // That's kinda redundant with net_util_unittests. 32 // That's kinda redundant with net_util_unittests.
33 const float available_width = 33 const float available_width =
34 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); 34 GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list);
35 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), 35 EXPECT_EQ(UTF8ToUTF16(testcases[i].output),
36 secure_display::ElideUrl(url, font_list, available_width, 36 url_formatter::ElideUrl(url, font_list, available_width,
37 std::string())); 37 std::string()));
38 } 38 }
39 } 39 }
40 40
41 // Test eliding of commonplace URLs. 41 // Test eliding of commonplace URLs.
42 TEST(TextEliderTest, TestGeneralEliding) { 42 TEST(TextEliderTest, TestGeneralEliding) {
43 const std::string kEllipsisStr(kEllipsis); 43 const std::string kEllipsisStr(kEllipsis);
44 Testcase testcases[] = { 44 Testcase testcases[] = {
45 {"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/"},
46 {"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/"},
47 {"http://www.google.com/intl/en/ads/", 47 {"http://www.google.com/intl/en/ads/",
(...skipping 30 matching lines...) Expand all
78 font_list); 78 font_list);
79 79
80 // Create the expected string, after elision. Depending on font size, the 80 // Create the expected string, after elision. Depending on font size, the
81 // directory might become /dir... or /di... or/d... - it never should be 81 // 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 82 // shorter than that. (If it is, the font considers d... to be longer
83 // than .../... - that should never happen). 83 // than .../... - that should never happen).
84 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), 84 ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
85 font_list), 85 font_list),
86 GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); 86 GetStringWidthF(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.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 =
203 GetStringWidthF(UTF8ToUTF16(testcases[i].output), gfx::FontList()); 203 GetStringWidthF(UTF8ToUTF16(testcases[i].output), gfx::FontList());
204 EXPECT_EQ(UTF8ToUTF16(testcases[i].output), 204 EXPECT_EQ(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

Powered by Google App Engine
This is Rietveld 408576698