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 IsNonForcedFull() OVERRIDE
{
25 return known_url_map_
.size() >= capacity_
;
27 virtual bool IsForcedFull() OVERRIDE
{
30 virtual bool IsKnownURL(const GURL
& url
) OVERRIDE
{
31 return known_url_map_
.find(url
.spec()) != known_url_map_
.end();
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()) {
40 *score
= iter
->second
;
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
;
51 virtual ~MockTopSites() {}
53 const size_t capacity_
;
54 std::map
<std::string
, ThumbnailScore
> known_url_map_
;
56 DISALLOW_COPY_AND_ASSIGN(MockTopSites
);
59 // A mock version of TestingProfile holds MockTopSites.
60 class MockProfile
: public TestingProfile
{
62 MockProfile() : mock_top_sites_(new MockTopSites(this)) {
65 virtual history::TopSites
* GetTopSites() OVERRIDE
{
66 return mock_top_sites_
.get();
69 void AddKnownURL(const GURL
& url
, const ThumbnailScore
& score
) {
70 mock_top_sites_
->AddKnownURL(url
, score
);
74 scoped_refptr
<MockTopSites
> mock_top_sites_
;
76 DISALLOW_COPY_AND_ASSIGN(MockProfile
);
79 TEST_F(ThumbnailServiceTest
, ShouldUpdateThumbnail
) {
80 const GURL
kGoodURL("http://www.google.com/");
81 const GURL
kBadURL("chrome://newtab");
83 // Set up the mock profile along with mock top sites.
84 base::ScopedTempDir temp_dir
;
85 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
88 scoped_refptr
<thumbnails::ThumbnailService
> thumbnail_service(
89 new thumbnails::ThumbnailServiceImpl(&profile
));
91 // Should be false because it's a bad URL.
92 EXPECT_FALSE(thumbnail_service
->ShouldAcquirePageThumbnail(kBadURL
));
94 // Should be true, as it's a good URL.
95 EXPECT_TRUE(thumbnail_service
->ShouldAcquirePageThumbnail(kGoodURL
));
97 // Not checking incognito mode since the service wouldn't have been created
98 // in that case anyway.
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());
106 // Should be false, as the top sites data is full, and the new URL is
108 const GURL
kAnotherGoodURL("http://www.youtube.com/");
109 EXPECT_FALSE(thumbnail_service
->ShouldAcquirePageThumbnail(kAnotherGoodURL
));
111 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
112 EXPECT_TRUE(thumbnail_service
->ShouldAcquirePageThumbnail(kGoodURL
));
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
);
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
));