1 // Copyright 2013 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.
5 #include "chrome/browser/history/url_utils.h"
7 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(HistoryUrlUtilsTest, CanonicalURLStringCompare) {
15 // Comprehensive test by comparing each pair in sorted list. O(n^2).
16 const char* sorted_list[] = {
17 "http://www.gogle.com/redirects_to_google",
18 "http://www.google.com",
19 "http://www.google.com/",
20 "http://www.google.com/?q",
21 "http://www.google.com/A",
22 "http://www.google.com/index.html",
23 "http://www.google.com/test",
24 "http://www.google.com/test?query",
25 "http://www.google.com/test?r=3",
26 "http://www.google.com/test#hash",
27 "http://www.google.com/test/?query",
28 "http://www.google.com/test/#hash",
29 "http://www.google.com/test/zzzzz",
30 "http://www.google.com/test$dollar",
31 "http://www.google.com/test%E9%9B%80",
32 "http://www.google.com/test-case",
33 "http://www.google.com:80/",
34 "https://www.google.com",
36 for (size_t i = 0; i < arraysize(sorted_list); ++i) {
37 EXPECT_FALSE(CanonicalURLStringCompare(sorted_list[i], sorted_list[i]))
38 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[i] << "\"";
39 // Every disjoint pair-wise comparison.
40 for (size_t j = i + 1; j < arraysize(sorted_list); ++j) {
41 EXPECT_TRUE(CanonicalURLStringCompare(sorted_list[i], sorted_list[j]))
42 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[j] << "\"";
43 EXPECT_FALSE(CanonicalURLStringCompare(sorted_list[j], sorted_list[i]))
44 << " for \"" << sorted_list[j] << "\" < \"" << sorted_list[i] << "\"";
49 TEST(HistoryUrlUtilsTest, HaveSameSchemeHostAndPort) {
54 {"http://www.google.com", "http://www.google.com"},
55 {"http://www.google.com/a/b", "http://www.google.com/a/b"},
56 {"http://www.google.com?test=3", "http://www.google.com/"},
57 {"http://www.google.com/#hash", "http://www.google.com/?q"},
58 {"http://www.google.com/", "http://www.google.com/test/with/dir/"},
59 {"http://www.google.com:360", "http://www.google.com:360/?q=1234"},
60 {"http://www.google.com:80", "http://www.google.com/gurl/is/smart"},
61 {"http://www.google.com/test", "http://www.google.com/test/with/dir/"},
62 {"http://www.google.com/test?", "http://www.google.com/test/with/dir/"},
64 for (size_t i = 0; i < arraysize(true_cases); ++i) {
65 EXPECT_TRUE(HaveSameSchemeHostAndPort(GURL(true_cases[i].s1),
66 GURL(true_cases[i].s2)))
67 << " for true_cases[" << i << "]";
73 {"http://www.google.co", "http://www.google.com"},
74 {"http://google.com", "http://www.google.com"},
75 {"http://www.google.com", "https://www.google.com"},
76 {"http://www.google.com/path", "http://www.google.com:137/path"},
77 {"http://www.google.com/same/dir", "http://www.youtube.com/same/dir"},
79 for (size_t i = 0; i < arraysize(false_cases); ++i) {
80 EXPECT_FALSE(HaveSameSchemeHostAndPort(GURL(false_cases[i].s1),
81 GURL(false_cases[i].s2)))
82 << " for false_cases[" << i << "]";
86 TEST(HistoryUrlUtilsTest, IsPathPrefix) {
95 {"/", "/test/with/dir/"},
96 {"/test", "/test/with/dir/"},
97 {"/test/", "/test/with/dir"},
99 for (size_t i = 0; i < arraysize(true_cases); ++i) {
100 EXPECT_TRUE(IsPathPrefix(true_cases[i].p1, true_cases[i].p2))
101 << " for true_cases[" << i << "]";
108 {"/", ""}, // Arguable.
109 {"/a/b/", "/a/b"}, // Arguable.
111 {"/test", "/test-bed"},
114 for (size_t i = 0; i < arraysize(false_cases); ++i) {
115 EXPECT_FALSE(IsPathPrefix(false_cases[i].p1, false_cases[i].p2))
116 << " for false_cases[" << i << "]";
120 TEST(HistoryUrlUtilsTest, ToggleHTTPAndHTTPS) {
121 EXPECT_EQ(GURL("http://www.google.com/test?q#r"),
122 ToggleHTTPAndHTTPS(GURL("https://www.google.com/test?q#r")));
123 EXPECT_EQ(GURL("https://www.google.com:137/"),
124 ToggleHTTPAndHTTPS(GURL("http://www.google.com:137/")));
125 EXPECT_EQ(GURL::EmptyGURL(),
126 ToggleHTTPAndHTTPS(GURL("ftp://www.google.com/")));
131 } // namespace history