Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / thumbnails / thumbnail_service_unittest.cc
blob32f564c37a3a8b9050ace37b824873a55f2f528f
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 {
17 public:
18 explicit MockTopSites(Profile* profile)
19 : history::TopSitesImpl(profile),
20 capacity_(1) {
23 // history::TopSitesImpl overrides.
24 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; }
25 bool IsForcedFull() override { return false; }
26 bool IsKnownURL(const GURL& url) override {
27 return known_url_map_.find(url.spec()) != known_url_map_.end();
29 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
30 std::map<std::string, ThumbnailScore>::const_iterator iter =
31 known_url_map_.find(url.spec());
32 if (iter == known_url_map_.end()) {
33 return false;
34 } else {
35 *score = iter->second;
36 return true;
40 // Adds a known URL with the associated thumbnail score.
41 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
42 known_url_map_[url.spec()] = score;
45 private:
46 ~MockTopSites() override {}
48 const size_t capacity_;
49 std::map<std::string, ThumbnailScore> known_url_map_;
51 DISALLOW_COPY_AND_ASSIGN(MockTopSites);
54 // A mock version of TestingProfile holds MockTopSites.
55 class MockProfile : public TestingProfile {
56 public:
57 MockProfile() : mock_top_sites_(new MockTopSites(this)) {
60 history::TopSites* GetTopSites() override { return mock_top_sites_.get(); }
62 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
63 mock_top_sites_->AddKnownURL(url, score);
66 private:
67 scoped_refptr<MockTopSites> mock_top_sites_;
69 DISALLOW_COPY_AND_ASSIGN(MockProfile);
72 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
73 const GURL kGoodURL("http://www.google.com/");
74 const GURL kBadURL("chrome://newtab");
76 // Set up the mock profile along with mock top sites.
77 base::ScopedTempDir temp_dir;
78 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
79 MockProfile profile;
81 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
82 new thumbnails::ThumbnailServiceImpl(&profile));
84 // Should be false because it's a bad URL.
85 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
87 // Should be true, as it's a good URL.
88 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
90 // Not checking incognito mode since the service wouldn't have been created
91 // in that case anyway.
93 // Add a known URL. This makes the top sites data full.
94 ThumbnailScore bad_score;
95 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
96 profile.AddKnownURL(kGoodURL, bad_score);
97 ASSERT_TRUE(profile.GetTopSites()->IsNonForcedFull());
99 // Should be false, as the top sites data is full, and the new URL is
100 // not known.
101 const GURL kAnotherGoodURL("http://www.youtube.com/");
102 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
104 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
105 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
107 // Replace the thumbnail score with a really good one.
108 ThumbnailScore good_score;
109 good_score.time_at_snapshot = base::Time::Now(); // Very new.
110 good_score.at_top = true;
111 good_score.good_clipping = true;
112 good_score.boring_score = 0.0;
113 good_score.load_completed = true;
114 profile.AddKnownURL(kGoodURL, good_score);
116 // Should be false, as the existing thumbnail is good enough (i.e. don't
117 // need to replace the existing thumbnail which is new and good).
118 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));