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.
5 #include "chrome/browser/thumbnails/thumbnail_service_impl.h"
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"
12 typedef testing::Test ThumbnailServiceTest;
14 // A mock version of TopSitesImpl, used for testing
15 // ShouldAcquirePageThumbnail().
16 class MockTopSites : public history::TopSitesImpl {
18 explicit MockTopSites(Profile* profile)
19 : history::TopSitesImpl(profile),
23 // history::TopSitesImpl overrides.
24 virtual bool IsFull() OVERRIDE {
25 return known_url_map_.size() >= capacity_;
27 virtual bool IsKnownURL(const GURL& url) OVERRIDE {
28 return known_url_map_.find(url.spec()) != known_url_map_.end();
30 virtual bool GetPageThumbnailScore(const GURL& url,
31 ThumbnailScore* score) OVERRIDE {
32 std::map<std::string, ThumbnailScore>::const_iterator iter =
33 known_url_map_.find(url.spec());
34 if (iter == known_url_map_.end()) {
37 *score = iter->second;
42 // Adds a known URL with the associated thumbnail score.
43 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
44 known_url_map_[url.spec()] = score;
48 virtual ~MockTopSites() {}
50 const size_t capacity_;
51 std::map<std::string, ThumbnailScore> known_url_map_;
53 DISALLOW_COPY_AND_ASSIGN(MockTopSites);
56 // A mock version of TestingProfile holds MockTopSites.
57 class MockProfile : public TestingProfile {
59 MockProfile() : mock_top_sites_(new MockTopSites(this)) {
62 virtual history::TopSites* GetTopSites() OVERRIDE {
63 return mock_top_sites_.get();
66 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
67 mock_top_sites_->AddKnownURL(url, score);
71 scoped_refptr<MockTopSites> mock_top_sites_;
73 DISALLOW_COPY_AND_ASSIGN(MockProfile);
76 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
77 const GURL kGoodURL("http://www.google.com/");
78 const GURL kBadURL("chrome://newtab");
80 // Set up the mock profile along with mock top sites.
81 base::ScopedTempDir temp_dir;
82 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
85 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
86 new thumbnails::ThumbnailServiceImpl(&profile));
88 // Should be false because it's a bad URL.
89 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
91 // Should be true, as it's a good URL.
92 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
94 // Not checking incognito mode since the service wouldn't have been created
95 // in that case anyway.
97 // Add a known URL. This makes the top sites data full.
98 ThumbnailScore bad_score;
99 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
100 profile.AddKnownURL(kGoodURL, bad_score);
101 ASSERT_TRUE(profile.GetTopSites()->IsFull());
103 // Should be false, as the top sites data is full, and the new URL is
105 const GURL kAnotherGoodURL("http://www.youtube.com/");
106 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
108 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
109 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
111 // Replace the thumbnail score with a really good one.
112 ThumbnailScore good_score;
113 good_score.time_at_snapshot = base::Time::Now(); // Very new.
114 good_score.at_top = true;
115 good_score.good_clipping = true;
116 good_score.boring_score = 0.0;
117 good_score.load_completed = true;
118 profile.AddKnownURL(kGoodURL, good_score);
120 // Should be false, as the existing thumbnail is good enough (i.e. don't
121 // need to replace the existing thumbnail which is new and good).
122 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));