Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / thumbnails / thumbnail_service_unittest.cc
blob435c82b81490c63bcbc829231c67a9f35daec103
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/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "chrome/browser/history/history_utils.h"
10 #include "chrome/browser/history/top_sites_factory.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/history/core/browser/top_sites_impl.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 class ThumbnailServiceTest : public testing::Test {
17 content::TestBrowserThreadBundle thread_bundle_;
20 namespace {
22 // A mock version of TopSitesImpl, used for testing
23 // ShouldAcquirePageThumbnail().
24 class MockTopSites : public history::TopSitesImpl {
25 public:
26 explicit MockTopSites(Profile* profile)
27 : history::TopSitesImpl(profile->GetPrefs(),
28 nullptr,
29 history::PrepopulatedPageList(),
30 base::Bind(CanAddURLToHistory)),
31 capacity_(1) {}
33 // history::TopSitesImpl overrides.
34 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; }
35 bool IsForcedFull() override { return false; }
36 bool IsKnownURL(const GURL& url) override {
37 return known_url_map_.find(url.spec()) != known_url_map_.end();
39 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
40 std::map<std::string, ThumbnailScore>::const_iterator iter =
41 known_url_map_.find(url.spec());
42 if (iter == known_url_map_.end()) {
43 return false;
44 } else {
45 *score = iter->second;
46 return true;
50 // Adds a known URL with the associated thumbnail score.
51 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
52 known_url_map_[url.spec()] = score;
55 private:
56 ~MockTopSites() override {}
58 const size_t capacity_;
59 std::map<std::string, ThumbnailScore> known_url_map_;
61 DISALLOW_COPY_AND_ASSIGN(MockTopSites);
64 // Testing factory that build a |MockTopSites| instance.
65 scoped_refptr<RefcountedKeyedService> BuildMockTopSites(
66 content::BrowserContext* profile) {
67 return scoped_refptr<RefcountedKeyedService>(
68 new MockTopSites(static_cast<Profile*>(profile)));
71 // A mock version of TestingProfile holds MockTopSites.
72 class MockProfile : public TestingProfile {
73 public:
74 MockProfile() {
75 TopSitesFactory::GetInstance()->SetTestingFactory(this, BuildMockTopSites);
78 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
79 scoped_refptr<history::TopSites> top_sites =
80 TopSitesFactory::GetForProfile(this);
81 static_cast<MockTopSites*>(top_sites.get())->AddKnownURL(url, score);
84 private:
86 DISALLOW_COPY_AND_ASSIGN(MockProfile);
89 } // namespace
91 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
92 const GURL kGoodURL("http://www.google.com/");
93 const GURL kBadURL("chrome://newtab");
95 // Set up the mock profile along with mock top sites.
96 base::ScopedTempDir temp_dir;
97 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
98 MockProfile profile;
100 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
101 new thumbnails::ThumbnailServiceImpl(&profile));
103 // Should be false because it's a bad URL.
104 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
106 // Should be true, as it's a good URL.
107 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
109 // Not checking incognito mode since the service wouldn't have been created
110 // in that case anyway.
112 // Add a known URL. This makes the top sites data full.
113 ThumbnailScore bad_score;
114 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
115 profile.AddKnownURL(kGoodURL, bad_score);
116 scoped_refptr<history::TopSites> top_sites =
117 TopSitesFactory::GetForProfile(&profile);
118 ASSERT_TRUE(top_sites->IsNonForcedFull());
120 // Should be false, as the top sites data is full, and the new URL is
121 // not known.
122 const GURL kAnotherGoodURL("http://www.youtube.com/");
123 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
125 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
126 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
128 // Replace the thumbnail score with a really good one.
129 ThumbnailScore good_score;
130 good_score.time_at_snapshot = base::Time::Now(); // Very new.
131 good_score.at_top = true;
132 good_score.good_clipping = true;
133 good_score.boring_score = 0.0;
134 good_score.load_completed = true;
135 profile.AddKnownURL(kGoodURL, good_score);
137 // Should be false, as the existing thumbnail is good enough (i.e. don't
138 // need to replace the existing thumbnail which is new and good).
139 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));