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