Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: chrome/browser/thumbnails/thumbnail_service_unittest.cc

Issue 815983002: Topsites become keyedService based. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove extra inclusion from testing_profile.h Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/thumbnails/thumbnail_service_impl.h" 5 #include "chrome/browser/thumbnails/thumbnail_service_impl.h"
6 6
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "chrome/browser/history/top_sites_factory.h"
8 #include "chrome/browser/history/top_sites_impl.h" 9 #include "chrome/browser/history/top_sites_impl.h"
9 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 typedef testing::Test ThumbnailServiceTest; 13 typedef testing::Test ThumbnailServiceTest;
13 14
15 namespace {
16
14 // A mock version of TopSitesImpl, used for testing 17 // A mock version of TopSitesImpl, used for testing
15 // ShouldAcquirePageThumbnail(). 18 // ShouldAcquirePageThumbnail().
16 class MockTopSites : public history::TopSitesImpl { 19 class MockTopSites : public history::TopSitesImpl {
17 public: 20 public:
18 explicit MockTopSites(Profile* profile) 21 explicit MockTopSites(Profile* profile)
19 : history::TopSitesImpl(profile), 22 : history::TopSitesImpl(profile), capacity_(1) {}
20 capacity_(1) {
21 }
22 23
23 // history::TopSitesImpl overrides. 24 // history::TopSitesImpl overrides.
24 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; } 25 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; }
25 bool IsForcedFull() override { return false; } 26 bool IsForcedFull() override { return false; }
26 bool IsKnownURL(const GURL& url) override { 27 bool IsKnownURL(const GURL& url) override {
27 return known_url_map_.find(url.spec()) != known_url_map_.end(); 28 return known_url_map_.find(url.spec()) != known_url_map_.end();
28 } 29 }
29 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override { 30 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
30 std::map<std::string, ThumbnailScore>::const_iterator iter = 31 std::map<std::string, ThumbnailScore>::const_iterator iter =
31 known_url_map_.find(url.spec()); 32 known_url_map_.find(url.spec());
(...skipping 12 matching lines...) Expand all
44 45
45 private: 46 private:
46 ~MockTopSites() override {} 47 ~MockTopSites() override {}
47 48
48 const size_t capacity_; 49 const size_t capacity_;
49 std::map<std::string, ThumbnailScore> known_url_map_; 50 std::map<std::string, ThumbnailScore> known_url_map_;
50 51
51 DISALLOW_COPY_AND_ASSIGN(MockTopSites); 52 DISALLOW_COPY_AND_ASSIGN(MockTopSites);
52 }; 53 };
53 54
55 // Testing factory that build a |MockTopSites| instance.
56 scoped_refptr<RefcountedKeyedService> BuildMockTopSites(
57 content::BrowserContext* profile) {
58 return scoped_refptr<RefcountedKeyedService>(
59 new MockTopSites(static_cast<Profile*>(profile)));
60 }
61
54 // A mock version of TestingProfile holds MockTopSites. 62 // A mock version of TestingProfile holds MockTopSites.
55 class MockProfile : public TestingProfile { 63 class MockProfile : public TestingProfile {
56 public: 64 public:
57 MockProfile() : mock_top_sites_(new MockTopSites(this)) { 65 MockProfile() {
66 TopSitesFactory::GetInstance()->SetTestingFactory(this, BuildMockTopSites);
58 } 67 }
59 68
60 history::TopSites* GetTopSites() override { return mock_top_sites_.get(); }
61
62 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { 69 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
63 mock_top_sites_->AddKnownURL(url, score); 70 scoped_refptr<history::TopSites> top_sites =
71 TopSitesFactory::GetForProfile(this);
72 static_cast<MockTopSites*>(top_sites.get())->AddKnownURL(url, score);
64 } 73 }
65 74
66 private: 75 private:
67 scoped_refptr<MockTopSites> mock_top_sites_;
68 76
69 DISALLOW_COPY_AND_ASSIGN(MockProfile); 77 DISALLOW_COPY_AND_ASSIGN(MockProfile);
70 }; 78 };
71 79
80 } // namespace
81
72 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) { 82 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
73 const GURL kGoodURL("http://www.google.com/"); 83 const GURL kGoodURL("http://www.google.com/");
74 const GURL kBadURL("chrome://newtab"); 84 const GURL kBadURL("chrome://newtab");
75 85
76 // Set up the mock profile along with mock top sites. 86 // Set up the mock profile along with mock top sites.
77 base::ScopedTempDir temp_dir; 87 base::ScopedTempDir temp_dir;
78 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 88 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
79 MockProfile profile; 89 MockProfile profile;
80 90
81 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service( 91 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
82 new thumbnails::ThumbnailServiceImpl(&profile)); 92 new thumbnails::ThumbnailServiceImpl(&profile));
83 93
84 // Should be false because it's a bad URL. 94 // Should be false because it's a bad URL.
85 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL)); 95 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
86 96
87 // Should be true, as it's a good URL. 97 // Should be true, as it's a good URL.
88 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL)); 98 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
89 99
90 // Not checking incognito mode since the service wouldn't have been created 100 // Not checking incognito mode since the service wouldn't have been created
91 // in that case anyway. 101 // in that case anyway.
92 102
93 // Add a known URL. This makes the top sites data full. 103 // Add a known URL. This makes the top sites data full.
94 ThumbnailScore bad_score; 104 ThumbnailScore bad_score;
95 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp. 105 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
96 profile.AddKnownURL(kGoodURL, bad_score); 106 profile.AddKnownURL(kGoodURL, bad_score);
97 ASSERT_TRUE(profile.GetTopSites()->IsNonForcedFull()); 107 scoped_refptr<history::TopSites> top_sites =
108 TopSitesFactory::GetForProfile(&profile);
109 ASSERT_TRUE(top_sites->IsNonForcedFull());
98 110
99 // Should be false, as the top sites data is full, and the new URL is 111 // Should be false, as the top sites data is full, and the new URL is
100 // not known. 112 // not known.
101 const GURL kAnotherGoodURL("http://www.youtube.com/"); 113 const GURL kAnotherGoodURL("http://www.youtube.com/");
102 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL)); 114 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
103 115
104 // Should be true, as the existing thumbnail is bad (i.e. need a better one). 116 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
105 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL)); 117 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
106 118
107 // Replace the thumbnail score with a really good one. 119 // Replace the thumbnail score with a really good one.
108 ThumbnailScore good_score; 120 ThumbnailScore good_score;
109 good_score.time_at_snapshot = base::Time::Now(); // Very new. 121 good_score.time_at_snapshot = base::Time::Now(); // Very new.
110 good_score.at_top = true; 122 good_score.at_top = true;
111 good_score.good_clipping = true; 123 good_score.good_clipping = true;
112 good_score.boring_score = 0.0; 124 good_score.boring_score = 0.0;
113 good_score.load_completed = true; 125 good_score.load_completed = true;
114 profile.AddKnownURL(kGoodURL, good_score); 126 profile.AddKnownURL(kGoodURL, good_score);
115 127
116 // Should be false, as the existing thumbnail is good enough (i.e. don't 128 // 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). 129 // need to replace the existing thumbnail which is new and good).
118 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL)); 130 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
119 } 131 }
OLDNEW
« no previous file with comments | « chrome/browser/thumbnails/thumbnail_service_impl.cc ('k') | chrome/browser/ui/app_list/test/fake_profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698