OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "chrome/browser/history/top_sites_cache.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/logging.h" | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace history { | |
16 | |
17 namespace { | |
18 | |
19 class TopSitesCacheTest : public testing::Test { | |
20 protected: | |
21 // Initializes |top_sites_| and |cache_| based on |spec|, which is a list of | |
22 // URL strings with optional indents: indentated URLs redirect to the last | |
23 // non-indented URL. Titles are assigned as "Title 1", "Title 2", etc., in the | |
24 // order of appearance. See |kTopSitesSpecBasic| for an example. | |
25 void InitTopSiteCache(char** spec, int size); | |
26 | |
27 MostVisitedURLList top_sites_; | |
28 TopSitesCache cache_; | |
29 }; | |
sky
2013/09/12 23:51:41
private: DIALLOW_...
huangs
2013/09/13 02:30:37
Done.
| |
30 | |
31 void TopSitesCacheTest::InitTopSiteCache(char** spec, int size) { | |
32 std::set<std::string> urls_seen; | |
33 for (int i = 0; i < size; ++i) { | |
34 char* spec_item = spec[i]; | |
35 while (*spec_item && *spec_item == ' ') // Eat indent. | |
36 ++spec_item; | |
37 ASSERT_EQ(urls_seen.find(spec_item), urls_seen.end()) | |
38 << "Duplicate URL found: " << spec_item; | |
39 urls_seen.insert(spec_item); | |
40 if (spec_item == spec[i]) { // No indent: add new MostVisitedURL. | |
41 string16 title(L"Title " + base::Uint64ToString16(top_sites_.size() + 1)); | |
42 top_sites_.push_back(MostVisitedURL(GURL(spec_item), title)); | |
43 } | |
44 ASSERT_TRUE(!top_sites_.empty()); | |
45 // Set up redirect to canonical URL. Canonical URL redirects to itself, too. | |
46 top_sites_.back().redirects.push_back(GURL(spec_item)); | |
47 } | |
48 cache_.SetTopSites(top_sites_); | |
49 } | |
50 | |
51 char* kTopSitesSpecBasic[] = { | |
52 "http://www.google.com", | |
53 " http://www.gogle.com", // Redirects. | |
54 " http://www.gooogle.com", // Redirects. | |
55 "http://www.youtube.com/a/b", | |
56 " http://www.youtube.com/a/b?test=1", // Redirects. | |
57 "https://www.google.com/", | |
58 " https://www.gogle.com", // Redirects. | |
59 "http://www.example.com:3141/", | |
60 }; | |
61 | |
62 TEST_F(TopSitesCacheTest, GetCanonicalURL) { | |
63 InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic)); | |
64 struct { | |
65 const char* expected; | |
66 const char* query; | |
67 } test_cases[] = { | |
68 // Already is canonical: redirects. | |
69 {"http://www.google.com/", "http://www.google.com"}, | |
70 // Exact match with stored URL: redirects. | |
71 {"http://www.google.com/", "http://www.gooogle.com"}, | |
72 // Recognizes despite trailing "/": redirects | |
73 {"http://www.google.com/", "http://www.gooogle.com/"}, | |
74 // Exact match with URL with query: redirects. | |
75 {"http://www.youtube.com/a/b", "http://www.youtube.com/a/b?test=1"}, | |
76 // No match with URL with query: as-is. | |
77 {"http://www.youtube.com/a/b?test", "http://www.youtube.com/a/b?test"}, | |
78 // Never-seen-before URL: as-is. | |
79 {"http://maps.google.com/", "http://maps.google.com/"}, | |
80 // Changing port number, does not match: as-is. | |
81 {"http://www.example.com:1234/", "http://www.example.com:1234"}, | |
82 // Smart enough to know that port 80 is HTTP: redirects. | |
83 {"http://www.google.com/", "http://www.gooogle.com:80"}, | |
84 // Prefix should not work: as-is. | |
85 {"http://www.youtube.com/a", "http://www.youtube.com/a"}, | |
86 }; | |
87 for (int i = 0; i < arraysize(test_cases); ++i) { | |
88 std::string expected(test_cases[i].expected); | |
89 std::string query(test_cases[i].query); | |
90 EXPECT_EQ(expected, cache_.GetCanonicalURL(GURL(query)).spec()) | |
91 << " for test_case[" << i << "]"; | |
92 } | |
93 } | |
94 | |
95 TEST_F(TopSitesCacheTest, IsKnownUrl) { | |
96 InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic)); | |
97 // Matches. | |
98 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com"))); | |
99 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.gooogle.com"))); | |
100 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com/"))); | |
101 | |
102 // Non-matches. | |
103 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com?"))); | |
104 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.net"))); | |
105 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com/stuff"))); | |
106 EXPECT_FALSE(cache_.IsKnownURL(GURL("https://www.gooogle.com"))); | |
107 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.youtube.com/a"))); | |
108 } | |
109 | |
110 char* kTopSitesSpecPrefix[] = { | |
111 "http://www.google.com/", | |
112 " http://www.google.com/test?q=3", // Redirects. | |
113 " http://www.google.com/test/y?b", // Redirects. | |
114 "http://www.google.com/2", | |
115 " http://www.google.com/test/q", // Redirects. | |
116 " http://www.google.com/test/y?a", // Redirects. | |
117 "http://www.google.com/3", | |
118 " http://www.google.com/testing", // Redirects. | |
119 "http://www.google.com/test-hyphen", | |
120 "http://www.google.com/sh", | |
121 " http://www.google.com/sh/1/2", // Redirects. | |
122 "http://www.google.com/sh/1", | |
123 }; | |
124 | |
125 TEST_F(TopSitesCacheTest, GetCanonicalURLForPrefix) { | |
126 InitTopSiteCache(kTopSitesSpecPrefix, arraysize(kTopSitesSpecPrefix)); | |
127 struct { | |
128 const char* expected; | |
129 const char* query; | |
130 } test_cases[] = { | |
131 // Already is canonical: redirects. | |
132 {"http://www.google.com/", "http://www.google.com"}, | |
133 // Exact match with stored URL: redirects. | |
134 {"http://www.google.com/", "http://www.google.com/test?q=3"}, | |
135 // Prefix match: redirects. | |
136 {"http://www.google.com/", "http://www.google.com/test"}, | |
137 // Competing prefix match: redirects to closest. | |
138 {"http://www.google.com/2", "http://www.google.com/test/q"}, | |
139 // Multiple prefix matches: redirects to first. | |
140 {"http://www.google.com/2", "http://www.google.com/test/y"}, | |
141 // No prefix match: as-is. | |
142 {"http://www.google.com/no-match", "http://www.google.com/no-match"}, | |
143 // String prefix match but not URL-prefix match: as-is. | |
144 {"http://www.google.com/t", "http://www.google.com/t"}, | |
145 // Different protocol: as-is. | |
146 {"https://www.google.com/test", "https://www.google.com/test"}, | |
147 // Smart enough to know that port 80 is HTTP: redirects. | |
148 {"http://www.google.com/", "http://www.google.com:80/test"}, | |
149 // Exact match, unaffected by "http://www.google.com/sh/1": redirects. | |
150 {"http://www.google.com/sh", "http://www.google.com/sh/1/2"}, | |
151 // Suffix match only: as-is | |
152 {"http://www.google.com/sh/1/2/3", "http://www.google.com/sh/1/2/3"}, | |
153 // Exact match, unaffected by "http://www.google.com/sh": redirects. | |
154 {"http://www.google.com/sh/1", "http://www.google.com/sh/1"}, | |
155 }; | |
156 for (int i = 0; i < arraysize(test_cases); ++i) { | |
157 std::string expected(test_cases[i].expected); | |
158 std::string query(test_cases[i].query); | |
159 EXPECT_EQ(expected, cache_.GetCanonicalURLForPrefix(GURL(query)).spec()) | |
160 << " for test_case[" << i << "]"; | |
161 } | |
162 } | |
163 | |
164 } // namespace | |
165 | |
166 } // namespace history | |
OLD | NEW |