OLD | NEW |
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_impl.h" | 8 #include "chrome/browser/history/top_sites_impl.h" |
9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 typedef testing::Test ThumbnailServiceTest; | 12 typedef testing::Test ThumbnailServiceTest; |
13 | 13 |
14 // A mock version of TopSitesImpl, used for testing | 14 // A mock version of TopSitesImpl, used for testing |
15 // ShouldAcquirePageThumbnail(). | 15 // ShouldAcquirePageThumbnail(). |
16 class MockTopSites : public history::TopSitesImpl { | 16 class MockTopSites : public history::TopSitesImpl { |
17 public: | 17 public: |
18 explicit MockTopSites(Profile* profile) | 18 explicit MockTopSites(Profile* profile) |
19 : history::TopSitesImpl(profile), | 19 : history::TopSitesImpl(profile), |
20 capacity_(1) { | 20 capacity_(1) { |
21 } | 21 } |
22 | 22 |
23 // history::TopSitesImpl overrides. | 23 // history::TopSitesImpl overrides. |
24 virtual bool IsNonForcedFull() override { | 24 bool IsNonForcedFull() override { return known_url_map_.size() >= capacity_; } |
25 return known_url_map_.size() >= capacity_; | 25 bool IsForcedFull() override { return false; } |
26 } | 26 bool IsKnownURL(const GURL& url) override { |
27 virtual bool IsForcedFull() override { | |
28 return false; | |
29 } | |
30 virtual bool IsKnownURL(const GURL& url) override { | |
31 return known_url_map_.find(url.spec()) != known_url_map_.end(); | 27 return known_url_map_.find(url.spec()) != known_url_map_.end(); |
32 } | 28 } |
33 virtual bool GetPageThumbnailScore(const GURL& url, | 29 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override { |
34 ThumbnailScore* score) override { | |
35 std::map<std::string, ThumbnailScore>::const_iterator iter = | 30 std::map<std::string, ThumbnailScore>::const_iterator iter = |
36 known_url_map_.find(url.spec()); | 31 known_url_map_.find(url.spec()); |
37 if (iter == known_url_map_.end()) { | 32 if (iter == known_url_map_.end()) { |
38 return false; | 33 return false; |
39 } else { | 34 } else { |
40 *score = iter->second; | 35 *score = iter->second; |
41 return true; | 36 return true; |
42 } | 37 } |
43 } | 38 } |
44 | 39 |
45 // Adds a known URL with the associated thumbnail score. | 40 // Adds a known URL with the associated thumbnail score. |
46 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { | 41 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { |
47 known_url_map_[url.spec()] = score; | 42 known_url_map_[url.spec()] = score; |
48 } | 43 } |
49 | 44 |
50 private: | 45 private: |
51 virtual ~MockTopSites() {} | 46 ~MockTopSites() override {} |
52 | 47 |
53 const size_t capacity_; | 48 const size_t capacity_; |
54 std::map<std::string, ThumbnailScore> known_url_map_; | 49 std::map<std::string, ThumbnailScore> known_url_map_; |
55 | 50 |
56 DISALLOW_COPY_AND_ASSIGN(MockTopSites); | 51 DISALLOW_COPY_AND_ASSIGN(MockTopSites); |
57 }; | 52 }; |
58 | 53 |
59 // A mock version of TestingProfile holds MockTopSites. | 54 // A mock version of TestingProfile holds MockTopSites. |
60 class MockProfile : public TestingProfile { | 55 class MockProfile : public TestingProfile { |
61 public: | 56 public: |
62 MockProfile() : mock_top_sites_(new MockTopSites(this)) { | 57 MockProfile() : mock_top_sites_(new MockTopSites(this)) { |
63 } | 58 } |
64 | 59 |
65 virtual history::TopSites* GetTopSites() override { | 60 history::TopSites* GetTopSites() override { return mock_top_sites_.get(); } |
66 return mock_top_sites_.get(); | |
67 } | |
68 | 61 |
69 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { | 62 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { |
70 mock_top_sites_->AddKnownURL(url, score); | 63 mock_top_sites_->AddKnownURL(url, score); |
71 } | 64 } |
72 | 65 |
73 private: | 66 private: |
74 scoped_refptr<MockTopSites> mock_top_sites_; | 67 scoped_refptr<MockTopSites> mock_top_sites_; |
75 | 68 |
76 DISALLOW_COPY_AND_ASSIGN(MockProfile); | 69 DISALLOW_COPY_AND_ASSIGN(MockProfile); |
77 }; | 70 }; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 good_score.at_top = true; | 110 good_score.at_top = true; |
118 good_score.good_clipping = true; | 111 good_score.good_clipping = true; |
119 good_score.boring_score = 0.0; | 112 good_score.boring_score = 0.0; |
120 good_score.load_completed = true; | 113 good_score.load_completed = true; |
121 profile.AddKnownURL(kGoodURL, good_score); | 114 profile.AddKnownURL(kGoodURL, good_score); |
122 | 115 |
123 // Should be false, as the existing thumbnail is good enough (i.e. don't | 116 // Should be false, as the existing thumbnail is good enough (i.e. don't |
124 // need to replace the existing thumbnail which is new and good). | 117 // need to replace the existing thumbnail which is new and good). |
125 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL)); | 118 EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL)); |
126 } | 119 } |
OLD | NEW |