Chromium Code Reviews| 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 #include "chrome/browser/history/top_sites_cache.h" | |
| 5 | |
| 6 #include <set> | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/scoped_ptr.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 }; | |
| 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 TEST_F(TopSitesCacheTest, CanonicalURLComparator) { | |
| 52 // Comprehensive test by comparing each pair in sorted list. O(n^2). | |
| 53 const char* sorted_list[] = { | |
| 54 "http://www.gogle.com/redirects_to_google", | |
| 55 "http://www.google.com", | |
| 56 "http://www.google.com/", | |
| 57 "http://www.google.com/?q", | |
| 58 "http://www.google.com/A", | |
| 59 "http://www.google.com/index.html", | |
| 60 "http://www.google.com/test", | |
| 61 "http://www.google.com/test?q=3", | |
| 62 "http://www.google.com/test?r=3", | |
| 63 "http://www.google.com/test/zzzzz", | |
| 64 "http://www.google.com/test-case", | |
| 65 "http://www.google.com:80/", | |
| 66 "https://www.google.com", | |
|
beaudoin
2013/09/04 16:46:10
Please throw in some weird characters that are val
huangs
2013/09/04 23:27:39
Done.
| |
| 67 }; | |
| 68 for (int i = 0; i < arraysize(sorted_list); ++i) { | |
| 69 EXPECT_FALSE( | |
| 70 CanonicalURLComparator::CompareString(sorted_list[i], sorted_list[i])) | |
| 71 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[i] << "\""; | |
| 72 // Every disjoint pair-wise comparison. | |
| 73 for (int j = i + 1; j < arraysize(sorted_list); ++j) { | |
| 74 EXPECT_TRUE( | |
| 75 CanonicalURLComparator::CompareString(sorted_list[i], sorted_list[j])) | |
| 76 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[j] << "\""; | |
| 77 EXPECT_FALSE( | |
| 78 CanonicalURLComparator::CompareString(sorted_list[j], sorted_list[i])) | |
| 79 << " for \"" << sorted_list[j] << "\" < \"" << sorted_list[i] << "\""; | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 char* kTopSitesSpecBasic[] = { | |
|
beaudoin
2013/09/04 16:46:10
Add a comment indicating an indent implies a redir
huangs
2013/09/04 23:27:39
Done.
| |
| 85 "http://www.google.com", | |
| 86 " http://www.gogle.com", | |
| 87 " http://www.gooogle.com", | |
| 88 "http://www.youtube.com/a/b", | |
| 89 " http://www.youtube.com/a/b?test=1", | |
| 90 "https://www.google.com", | |
| 91 " https://www.gogle.com", | |
| 92 "http://www.example.com:3141", | |
| 93 }; | |
| 94 | |
| 95 TEST_F(TopSitesCacheTest, GetCanonicalURL) { | |
| 96 InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic)); | |
| 97 // Already is canonical: redirects. | |
| 98 EXPECT_EQ(GURL("http://www.google.com"), | |
|
huangs
2013/09/04 23:27:39
Instead of comparing GURL(x) with GURL(y), it's mo
| |
| 99 cache_.GetCanonicalURL(GURL("http://www.google.com"))); | |
| 100 // Exact match with stored URL: redirects. | |
| 101 EXPECT_EQ(GURL("http://www.google.com"), | |
| 102 cache_.GetCanonicalURL(GURL("http://www.gooogle.com"))); | |
| 103 // Recognizes despite trailing "/". | |
| 104 EXPECT_EQ(GURL("http://www.google.com"), | |
| 105 cache_.GetCanonicalURL(GURL("http://www.gooogle.com/"))); | |
| 106 // Exact match with URL with query: redirects. | |
| 107 EXPECT_EQ(GURL("http://www.youtube.com/a/b"), | |
| 108 cache_.GetCanonicalURL(GURL("http://www.youtube.com/a/b?test=1"))); | |
| 109 // No match with URL with query: as-is. | |
| 110 EXPECT_EQ(GURL("http://www.youtube.com/a/b?test"), | |
| 111 cache_.GetCanonicalURL(GURL("http://www.youtube.com/a/b?test"))); | |
| 112 // Never-seen-before URL: as-is. | |
| 113 EXPECT_EQ(GURL("http://maps.google.com/"), | |
| 114 cache_.GetCanonicalURL(GURL("http://maps.google.com/"))); | |
| 115 // Changing port number, does not match: as-is. | |
| 116 EXPECT_EQ(GURL("http://www.example.com:80"), | |
| 117 cache_.GetCanonicalURL(GURL("http://www.example.com:80"))); | |
|
beaudoin
2013/09/04 16:46:10
// Prefix doesn't work.
EXPECT_EQ(GURL("http://www
huangs
2013/09/04 23:27:39
Done.
| |
| 118 } | |
| 119 | |
| 120 TEST_F(TopSitesCacheTest, IsKnownUrl) { | |
| 121 InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic)); | |
| 122 // Matches. | |
| 123 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com"))); | |
| 124 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.gooogle.com"))); | |
| 125 EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com/"))); | |
| 126 | |
| 127 // Non-matches. | |
| 128 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com?"))); | |
| 129 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.net"))); | |
| 130 EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com/stuff"))); | |
| 131 EXPECT_FALSE(cache_.IsKnownURL(GURL("https://www.gooogle.com"))); | |
|
beaudoin
2013/09/04 16:46:10
EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.yo
huangs
2013/09/04 23:27:39
Done.
| |
| 132 } | |
| 133 | |
| 134 char* kTopSitesSpecPrefix[] = { | |
|
beaudoin
2013/09/04 16:46:10
Same short comment.
huangs
2013/09/04 23:27:39
Done.
| |
| 135 "http://www.g.com", | |
| 136 " http://www.g.com/test?q=3", | |
| 137 " http://www.g.com/test/y?b", | |
| 138 "http://www.g.com/2", | |
| 139 " http://www.g.com/test/q", | |
| 140 " http://www.g.com/test/y?a", | |
| 141 "http://www.g.com/3", | |
| 142 " http://www.g.com/testing", | |
| 143 "http://www.g.com/test-hyphen", | |
|
beaudoin
2013/09/04 16:46:10
I would add:
"http://www.g.com/test",
Without an
huangs
2013/09/04 23:27:39
Don't want to interfere with existing test, but ad
| |
| 144 }; | |
| 145 | |
| 146 TEST_F(TopSitesCacheTest, GetCanonicalURLForPrefix) { | |
| 147 InitTopSiteCache(kTopSitesSpecPrefix, arraysize(kTopSitesSpecPrefix)); | |
| 148 // Already is canonical: redirects. | |
| 149 EXPECT_EQ(GURL("http://www.g.com"), | |
| 150 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com"))); | |
| 151 // Exact match with stored URL: redirects. | |
| 152 EXPECT_EQ(GURL("http://www.g.com"), | |
| 153 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/test?q=3"))); | |
| 154 // Prefix match: redirects. | |
| 155 EXPECT_EQ(GURL("http://www.g.com"), | |
| 156 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/test"))); | |
| 157 // Competing prefix match: redirects to closest. | |
| 158 EXPECT_EQ(GURL("http://www.g.com/2"), | |
| 159 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/test/q"))); | |
| 160 // Multiple prefix matches: redirects to first. | |
| 161 EXPECT_EQ(GURL("http://www.g.com/2"), | |
| 162 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/test/y"))); | |
| 163 // No prefix match: as-is | |
| 164 EXPECT_EQ(GURL("http://www.g.com/no-match"), | |
| 165 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/no-match"))); | |
| 166 // String prefix match but not URL-prefix match: as-is. | |
| 167 EXPECT_EQ(GURL("http://www.g.com/t"), | |
| 168 cache_.GetCanonicalURLForPrefix(GURL("http://www.g.com/t"))); | |
| 169 // Different protocol: as-is. | |
| 170 EXPECT_EQ(GURL("https://www.g.com/test"), | |
| 171 cache_.GetCanonicalURLForPrefix(GURL("https://www.g.com/test"))); | |
| 172 } | |
| 173 | |
| 174 TEST_F(TopSitesCacheTest, UrlStringIsPrefix) { | |
| 175 struct { | |
| 176 const char* s1; | |
| 177 const char* s2; | |
| 178 } true_cases[] = { | |
| 179 {"http://www.google.com", "http://www.google.com"}, | |
| 180 {"http://www.google.com/a/b", "http://www.google.com/a/b"}, | |
| 181 {"http://www.google.com?test=3", "http://www.google.com/"}, | |
| 182 {"http://www.google.com/", "http://www.google.com/test/with/dir/"}, | |
| 183 {"http://www.google.com:360", "http://www.google.com:360/?q=1234"}, | |
| 184 {"http://www.google.com:80", "http://www.google.com/gurl/is/smart"}, | |
|
beaudoin
2013/09/04 16:46:10
This case will work with UrlStringIsPrefix but may
huangs
2013/09/04 23:27:39
It looks like GURL is smart enough:
GURL("http://
beaudoin
2013/09/05 01:41:19
Ack.
| |
| 185 {"http://www.google.com/test", "http://www.google.com/test/with/dir/"}, | |
| 186 {"http://www.google.com/test/", "http://www.google.com/test/with/dir"}, | |
| 187 {"http://www.google.com/test?", "http://www.google.com/test/with/dir/"}, | |
| 188 }; | |
| 189 for (int i = 0; i < arraysize(true_cases); ++i) { | |
| 190 EXPECT_TRUE(TopSitesCache::UrlIsPrefix(GURL(true_cases[i].s1), | |
| 191 GURL(true_cases[i].s2))) | |
| 192 << " for true_cases[" << i << "]"; | |
| 193 } | |
| 194 struct { | |
| 195 const char* s1; | |
| 196 const char* s2; | |
| 197 } false_cases[] = { | |
| 198 {"http://www.google.com/test", "http://www.google.com"}, | |
| 199 {"http://www.google.com/a/b/", "http://www.google.com/a/b"}, // Arguable. | |
| 200 {"http://www.google.co", "http://www.google.com"}, | |
| 201 {"http://google.com", "http://www.google.com"}, | |
| 202 {"http://www.google.com", "https://www.google.com"}, | |
| 203 {"http://www.google.com/path", "http://www.google.com:137/path"}, | |
| 204 {"http://www.google.com/same/dir", "http://www.youtube.com/same/dir"}, | |
| 205 {"http://www.google.com/te", "http://www.google.com/test"}, | |
| 206 {"http://www.google.com/test", "http://www.google.com/test-bed"}, | |
| 207 {"http://www.google.com/test-", "http://www.google.com/test?"}, | |
| 208 }; | |
| 209 for (int i = 0; i < arraysize(false_cases); ++i) { | |
| 210 EXPECT_FALSE(TopSitesCache::UrlIsPrefix(GURL(false_cases[i].s1), | |
| 211 GURL(false_cases[i].s2))) | |
| 212 << " for false_cases[" << i << "]"; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 } // namespace | |
| 217 | |
| 218 } // namespace history | |
| OLD | NEW |