| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/history/top_sites_cache.h" | 5 #include "chrome/browser/history/top_sites_cache.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace history { | 16 namespace history { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class TopSitesCacheTest : public testing::Test { | 20 class TopSitesCacheTest : public testing::Test { |
| 21 public: | 21 public: |
| 22 TopSitesCacheTest() { | 22 TopSitesCacheTest() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 protected: | 25 protected: |
| 26 // Initializes |top_sites_| and |cache_| based on |spec|, which is a list of | 26 // Initializes |top_sites_| on |spec|, which is a list of URL strings with |
| 27 // URL strings with optional indents: indentated URLs redirect to the last | 27 // optional indents: indentated URLs redirect to the last non-indented URL. |
| 28 // non-indented URL. Titles are assigned as "Title 1", "Title 2", etc., in the | 28 // Titles are assigned as "Title 1", "Title 2", etc., in the order of |
| 29 // order of appearance. See |kTopSitesSpecBasic| for an example. | 29 // appearance. See |kTopSitesSpecBasic| for an example. This function does not |
| 30 // update |cache_| so you can manipulate |top_sites_| before you update it. |
| 31 void BuildTopSites(const char** spec, size_t size); |
| 32 |
| 33 // Initializes |top_sites_| and |cache_| based on |spec|. |
| 30 void InitTopSiteCache(const char** spec, size_t size); | 34 void InitTopSiteCache(const char** spec, size_t size); |
| 31 | 35 |
| 32 MostVisitedURLList top_sites_; | 36 MostVisitedURLList top_sites_; |
| 33 TopSitesCache cache_; | 37 TopSitesCache cache_; |
| 34 | 38 |
| 35 private: | 39 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(TopSitesCacheTest); | 40 DISALLOW_COPY_AND_ASSIGN(TopSitesCacheTest); |
| 37 }; | 41 }; |
| 38 | 42 |
| 39 void TopSitesCacheTest::InitTopSiteCache(const char** spec, size_t size) { | 43 void TopSitesCacheTest::BuildTopSites(const char** spec, size_t size) { |
| 40 std::set<std::string> urls_seen; | 44 std::set<std::string> urls_seen; |
| 41 for (size_t i = 0; i < size; ++i) { | 45 for (size_t i = 0; i < size; ++i) { |
| 42 const char* spec_item = spec[i]; | 46 const char* spec_item = spec[i]; |
| 43 while (*spec_item && *spec_item == ' ') // Eat indent. | 47 while (*spec_item && *spec_item == ' ') // Eat indent. |
| 44 ++spec_item; | 48 ++spec_item; |
| 45 if (urls_seen.find(spec_item) != urls_seen.end()) | 49 if (urls_seen.find(spec_item) != urls_seen.end()) |
| 46 NOTREACHED() << "Duplicate URL found: " << spec_item; | 50 NOTREACHED() << "Duplicate URL found: " << spec_item; |
| 47 urls_seen.insert(spec_item); | 51 urls_seen.insert(spec_item); |
| 48 if (spec_item == spec[i]) { // No indent: add new MostVisitedURL. | 52 if (spec_item == spec[i]) { // No indent: add new MostVisitedURL. |
| 49 string16 title(ASCIIToUTF16("Title ") + | 53 string16 title(ASCIIToUTF16("Title ") + |
| 50 base::Uint64ToString16(top_sites_.size() + 1)); | 54 base::Uint64ToString16(top_sites_.size() + 1)); |
| 51 top_sites_.push_back(MostVisitedURL(GURL(spec_item), title)); | 55 top_sites_.push_back(MostVisitedURL(GURL(spec_item), title)); |
| 52 } | 56 } |
| 53 ASSERT_TRUE(!top_sites_.empty()); | 57 ASSERT_TRUE(!top_sites_.empty()); |
| 54 // Set up redirect to canonical URL. Canonical URL redirects to itself, too. | 58 // Set up redirect to canonical URL. Canonical URL redirects to itself, too. |
| 55 top_sites_.back().redirects.push_back(GURL(spec_item)); | 59 top_sites_.back().redirects.push_back(GURL(spec_item)); |
| 56 } | 60 } |
| 61 } |
| 62 |
| 63 void TopSitesCacheTest::InitTopSiteCache(const char** spec, size_t size) { |
| 64 BuildTopSites(spec, size); |
| 57 cache_.SetTopSites(top_sites_); | 65 cache_.SetTopSites(top_sites_); |
| 58 } | 66 } |
| 59 | 67 |
| 60 const char* kTopSitesSpecBasic[] = { | 68 const char* kTopSitesSpecBasic[] = { |
| 61 "http://www.google.com", | 69 "http://www.google.com", |
| 62 " http://www.gogle.com", // Redirects. | 70 " http://www.gogle.com", // Redirects. |
| 63 " http://www.gooogle.com", // Redirects. | 71 " http://www.gooogle.com", // Redirects. |
| 64 "http://www.youtube.com/a/b", | 72 "http://www.youtube.com/a/b", |
| 65 " http://www.youtube.com/a/b?test=1", // Redirects. | 73 " http://www.youtube.com/a/b?test=1", // Redirects. |
| 66 "https://www.google.com/", | 74 "https://www.google.com/", |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 277 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 270 std::string expected(test_cases[i].expected); | 278 std::string expected(test_cases[i].expected); |
| 271 std::string query(test_cases[i].query); | 279 std::string query(test_cases[i].query); |
| 272 GURL result1(cache_.GetSpecializedCanonicalURL(GURL(query))); | 280 GURL result1(cache_.GetSpecializedCanonicalURL(GURL(query))); |
| 273 EXPECT_EQ(expected, result1.spec()) << " for test_case[" << i << "]"; | 281 EXPECT_EQ(expected, result1.spec()) << " for test_case[" << i << "]"; |
| 274 GURL result2(cache_.GetGeneralizedCanonicalURL(GURL(query))); | 282 GURL result2(cache_.GetGeneralizedCanonicalURL(GURL(query))); |
| 275 EXPECT_EQ(expected, result2.spec()) << " for test_case[" << i << "]"; | 283 EXPECT_EQ(expected, result2.spec()) << " for test_case[" << i << "]"; |
| 276 } | 284 } |
| 277 } | 285 } |
| 278 | 286 |
| 287 // This test ensures forced URLs behave in the expected way. |
| 288 TEST_F(TopSitesCacheTest, CacheForcedURLs) { |
| 289 // Forced URLs must always appear at the beginning of the list. |
| 290 BuildTopSites(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic)); |
| 291 top_sites_[0].last_forced_time = base::Time::FromJsTime(1000); |
| 292 top_sites_[1].last_forced_time = base::Time::FromJsTime(2000); |
| 293 cache_.SetTopSites(top_sites_); |
| 294 |
| 295 EXPECT_EQ(2u, cache_.GetNbForcedURLs()); |
| 296 EXPECT_EQ(2u, cache_.GetNbNonForcedURLs()); |
| 297 } |
| 298 |
| 279 } // namespace | 299 } // namespace |
| 280 | 300 |
| 281 } // namespace history | 301 } // namespace history |
| OLD | NEW |