This source file includes following definitions.
- capacity_
- IsNonForcedFull
- IsForcedFull
- IsKnownURL
- GetPageThumbnailScore
- AddKnownURL
- GetTopSites
- AddKnownURL
- TEST_F
#include "chrome/browser/thumbnails/thumbnail_service_impl.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/history/top_sites_impl.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
typedef testing::Test ThumbnailServiceTest;
class MockTopSites : public history::TopSitesImpl {
public:
explicit MockTopSites(Profile* profile)
: history::TopSitesImpl(profile),
capacity_(1) {
}
virtual bool IsNonForcedFull() OVERRIDE {
return known_url_map_.size() >= capacity_;
}
virtual bool IsForcedFull() OVERRIDE {
return false;
}
virtual bool IsKnownURL(const GURL& url) OVERRIDE {
return known_url_map_.find(url.spec()) != known_url_map_.end();
}
virtual bool GetPageThumbnailScore(const GURL& url,
ThumbnailScore* score) OVERRIDE {
std::map<std::string, ThumbnailScore>::const_iterator iter =
known_url_map_.find(url.spec());
if (iter == known_url_map_.end()) {
return false;
} else {
*score = iter->second;
return true;
}
}
void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
known_url_map_[url.spec()] = score;
}
private:
virtual ~MockTopSites() {}
const size_t capacity_;
std::map<std::string, ThumbnailScore> known_url_map_;
DISALLOW_COPY_AND_ASSIGN(MockTopSites);
};
class MockProfile : public TestingProfile {
public:
MockProfile() : mock_top_sites_(new MockTopSites(this)) {
}
virtual history::TopSites* GetTopSites() OVERRIDE {
return mock_top_sites_.get();
}
void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
mock_top_sites_->AddKnownURL(url, score);
}
private:
scoped_refptr<MockTopSites> mock_top_sites_;
DISALLOW_COPY_AND_ASSIGN(MockProfile);
};
TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
const GURL kGoodURL("http://www.google.com/");
const GURL kBadURL("chrome://newtab");
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
MockProfile profile;
scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
new thumbnails::ThumbnailServiceImpl(&profile));
EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
ThumbnailScore bad_score;
bad_score.time_at_snapshot = base::Time::UnixEpoch();
profile.AddKnownURL(kGoodURL, bad_score);
ASSERT_TRUE(profile.GetTopSites()->IsNonForcedFull());
const GURL kAnotherGoodURL("http://www.youtube.com/");
EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
ThumbnailScore good_score;
good_score.time_at_snapshot = base::Time::Now();
good_score.at_top = true;
good_score.good_clipping = true;
good_score.boring_score = 0.0;
good_score.load_completed = true;
profile.AddKnownURL(kGoodURL, good_score);
EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
}