Home | History | Annotate | Download | only in thumbnails
      1 // Copyright (c) 2012 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/thumbnails/thumbnail_service_impl.h"
      6 
      7 #include "base/memory/ref_counted.h"
      8 #include "chrome/browser/history/top_sites_impl.h"
      9 #include "chrome/test/base/testing_profile.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 typedef testing::Test ThumbnailServiceTest;
     13 
     14 // A mock version of TopSitesImpl, used for testing
     15 // ShouldAcquirePageThumbnail().
     16 class MockTopSites : public history::TopSitesImpl {
     17  public:
     18   explicit MockTopSites(Profile* profile)
     19       : history::TopSitesImpl(profile),
     20         capacity_(1) {
     21   }
     22 
     23   // history::TopSitesImpl overrides.
     24   virtual bool IsNonForcedFull() OVERRIDE {
     25     return known_url_map_.size() >= capacity_;
     26   }
     27   virtual bool IsForcedFull() OVERRIDE {
     28     return false;
     29   }
     30   virtual bool IsKnownURL(const GURL& url) OVERRIDE {
     31     return known_url_map_.find(url.spec()) != known_url_map_.end();
     32   }
     33   virtual bool GetPageThumbnailScore(const GURL& url,
     34                                      ThumbnailScore* score) OVERRIDE {
     35     std::map<std::string, ThumbnailScore>::const_iterator iter =
     36         known_url_map_.find(url.spec());
     37     if (iter == known_url_map_.end()) {
     38       return false;
     39     } else {
     40       *score = iter->second;
     41       return true;
     42     }
     43   }
     44 
     45   // Adds a known URL with the associated thumbnail score.
     46   void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
     47     known_url_map_[url.spec()] = score;
     48   }
     49 
     50  private:
     51   virtual ~MockTopSites() {}
     52 
     53   const size_t capacity_;
     54   std::map<std::string, ThumbnailScore> known_url_map_;
     55 
     56   DISALLOW_COPY_AND_ASSIGN(MockTopSites);
     57 };
     58 
     59 // A mock version of TestingProfile holds MockTopSites.
     60 class MockProfile : public TestingProfile {
     61  public:
     62   MockProfile() : mock_top_sites_(new MockTopSites(this)) {
     63   }
     64 
     65   virtual history::TopSites* GetTopSites() OVERRIDE {
     66     return mock_top_sites_.get();
     67   }
     68 
     69   void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
     70     mock_top_sites_->AddKnownURL(url, score);
     71   }
     72 
     73  private:
     74   scoped_refptr<MockTopSites> mock_top_sites_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(MockProfile);
     77 };
     78 
     79 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
     80   const GURL kGoodURL("http://www.google.com/");
     81   const GURL kBadURL("chrome://newtab");
     82 
     83   // Set up the mock profile along with mock top sites.
     84   base::ScopedTempDir temp_dir;
     85   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
     86   MockProfile profile;
     87 
     88   scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
     89       new thumbnails::ThumbnailServiceImpl(&profile));
     90 
     91   // Should be false because it's a bad URL.
     92   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
     93 
     94   // Should be true, as it's a good URL.
     95   EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
     96 
     97   // Not checking incognito mode since the service wouldn't have been created
     98   // in that case anyway.
     99 
    100   // Add a known URL. This makes the top sites data full.
    101   ThumbnailScore bad_score;
    102   bad_score.time_at_snapshot = base::Time::UnixEpoch();  // Ancient time stamp.
    103   profile.AddKnownURL(kGoodURL, bad_score);
    104   ASSERT_TRUE(profile.GetTopSites()->IsNonForcedFull());
    105 
    106   // Should be false, as the top sites data is full, and the new URL is
    107   // not known.
    108   const GURL kAnotherGoodURL("http://www.youtube.com/");
    109   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
    110 
    111   // Should be true, as the existing thumbnail is bad (i.e. need a better one).
    112   EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
    113 
    114   // Replace the thumbnail score with a really good one.
    115   ThumbnailScore good_score;
    116   good_score.time_at_snapshot = base::Time::Now();  // Very new.
    117   good_score.at_top = true;
    118   good_score.good_clipping = true;
    119   good_score.boring_score = 0.0;
    120   good_score.load_completed = true;
    121   profile.AddKnownURL(kGoodURL, good_score);
    122 
    123   // Should be false, as the existing thumbnail is good enough (i.e. don't
    124   // need to replace the existing thumbnail which is new and good).
    125   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
    126 }
    127