Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / thumbnails / thumbnail_service_unittest.cc
blobe900d44bb92a2f371b70a832aeb314d6616f506c
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_factory.h"
9 #include "chrome/browser/history/top_sites_impl.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 typedef testing::Test ThumbnailServiceTest;
15 namespace {
17 // A mock version of TopSitesImpl, used for testing
18 // ShouldAcquirePageThumbnail().
19 class MockTopSites : public history::TopSitesImpl {
20 public:
21 explicit MockTopSites(Profile* profile)
22 : history::TopSitesImpl(profile, history::PrepopulatedPageList()),
23 capacity_(1) {}
25 // history::TopSitesImpl overrides.
26 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; }
27 bool IsForcedFull() override { return false; }
28 bool IsKnownURL(const GURL& url) override {
29 return known_url_map_.find(url.spec()) != known_url_map_.end();
31 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
32 std::map<std::string, ThumbnailScore>::const_iterator iter =
33 known_url_map_.find(url.spec());
34 if (iter == known_url_map_.end()) {
35 return false;
36 } else {
37 *score = iter->second;
38 return true;
42 // Adds a known URL with the associated thumbnail score.
43 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
44 known_url_map_[url.spec()] = score;
47 private:
48 ~MockTopSites() override {}
50 const size_t capacity_;
51 std::map<std::string, ThumbnailScore> known_url_map_;
53 DISALLOW_COPY_AND_ASSIGN(MockTopSites);
56 // Testing factory that build a |MockTopSites| instance.
57 scoped_refptr<RefcountedKeyedService> BuildMockTopSites(
58 content::BrowserContext* profile) {
59 return scoped_refptr<RefcountedKeyedService>(
60 new MockTopSites(static_cast<Profile*>(profile)));
63 // A mock version of TestingProfile holds MockTopSites.
64 class MockProfile : public TestingProfile {
65 public:
66 MockProfile() {
67 TopSitesFactory::GetInstance()->SetTestingFactory(this, BuildMockTopSites);
70 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
71 scoped_refptr<history::TopSites> top_sites =
72 TopSitesFactory::GetForProfile(this);
73 static_cast<MockTopSites*>(top_sites.get())->AddKnownURL(url, score);
76 private:
78 DISALLOW_COPY_AND_ASSIGN(MockProfile);
81 } // namespace
83 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
84 const GURL kGoodURL("http://www.google.com/");
85 const GURL kBadURL("chrome://newtab");
87 // Set up the mock profile along with mock top sites.
88 base::ScopedTempDir temp_dir;
89 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
90 MockProfile profile;
92 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
93 new thumbnails::ThumbnailServiceImpl(&profile));
95 // Should be false because it's a bad URL.
96 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
98 // Should be true, as it's a good URL.
99 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
101 // Not checking incognito mode since the service wouldn't have been created
102 // in that case anyway.
104 // Add a known URL. This makes the top sites data full.
105 ThumbnailScore bad_score;
106 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
107 profile.AddKnownURL(kGoodURL, bad_score);
108 scoped_refptr<history::TopSites> top_sites =
109 TopSitesFactory::GetForProfile(&profile);
110 ASSERT_TRUE(top_sites->IsNonForcedFull());
112 // Should be false, as the top sites data is full, and the new URL is
113 // not known.
114 const GURL kAnotherGoodURL("http://www.youtube.com/");
115 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
117 // Should be true, as the existing thumbnail is bad (i.e. need a better one).
118 EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
120 // Replace the thumbnail score with a really good one.
121 ThumbnailScore good_score;
122 good_score.time_at_snapshot = base::Time::Now(); // Very new.
123 good_score.at_top = true;
124 good_score.good_clipping = true;
125 good_score.boring_score = 0.0;
126 good_score.load_completed = true;
127 profile.AddKnownURL(kGoodURL, good_score);
129 // Should be false, as the existing thumbnail is good enough (i.e. don't
130 // need to replace the existing thumbnail which is new and good).
131 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));