- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / thumbnails / thumbnail_service_unittest.cc
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.
4
5 #include "chrome/browser/thumbnails/thumbnail_service_impl.h"
6
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"
11
12 typedef testing::Test ThumbnailServiceTest;
13
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) {
21   }
22
23   // history::TopSitesImpl overrides.
24   virtual bool IsFull() OVERRIDE {
25     return known_url_map_.size() >= capacity_;
26   }
27   virtual bool IsKnownURL(const GURL& url) OVERRIDE {
28     return known_url_map_.find(url.spec()) != known_url_map_.end();
29   }
30   virtual bool GetPageThumbnailScore(const GURL& url,
31                                      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;
39     }
40   }
41
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;
45   }
46
47  private:
48   virtual ~MockTopSites() {}
49
50   const size_t capacity_;
51   std::map<std::string, ThumbnailScore> known_url_map_;
52
53   DISALLOW_COPY_AND_ASSIGN(MockTopSites);
54 };
55
56 // A mock version of TestingProfile holds MockTopSites.
57 class MockProfile : public TestingProfile {
58  public:
59   MockProfile() : mock_top_sites_(new MockTopSites(this)) {
60   }
61
62   virtual history::TopSites* GetTopSites() OVERRIDE {
63     return mock_top_sites_.get();
64   }
65
66   void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
67     mock_top_sites_->AddKnownURL(url, score);
68   }
69
70  private:
71   scoped_refptr<MockTopSites> mock_top_sites_;
72
73   DISALLOW_COPY_AND_ASSIGN(MockProfile);
74 };
75
76 TEST_F(ThumbnailServiceTest, ShouldUpdateThumbnail) {
77   const GURL kGoodURL("http://www.google.com/");
78   const GURL kBadURL("chrome://newtab");
79
80   // Set up the mock profile along with mock top sites.
81   base::ScopedTempDir temp_dir;
82   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
83   MockProfile profile;
84
85   scoped_refptr<thumbnails::ThumbnailService> thumbnail_service(
86       new thumbnails::ThumbnailServiceImpl(&profile));
87
88   // Should be false because it's a bad URL.
89   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kBadURL));
90
91   // Should be true, as it's a good URL.
92   EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
93
94   // Not checking incognito mode since the service wouldn't have been created
95   // in that case anyway.
96
97   // Add a known URL. This makes the top sites data full.
98   ThumbnailScore bad_score;
99   bad_score.time_at_snapshot = base::Time::UnixEpoch();  // Ancient time stamp.
100   profile.AddKnownURL(kGoodURL, bad_score);
101   ASSERT_TRUE(profile.GetTopSites()->IsFull());
102
103   // Should be false, as the top sites data is full, and the new URL is
104   // not known.
105   const GURL kAnotherGoodURL("http://www.youtube.com/");
106   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kAnotherGoodURL));
107
108   // Should be true, as the existing thumbnail is bad (i.e. need a better one).
109   EXPECT_TRUE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
110
111   // Replace the thumbnail score with a really good one.
112   ThumbnailScore good_score;
113   good_score.time_at_snapshot = base::Time::Now();  // Very new.
114   good_score.at_top = true;
115   good_score.good_clipping = true;
116   good_score.boring_score = 0.0;
117   good_score.load_completed = true;
118   profile.AddKnownURL(kGoodURL, good_score);
119
120   // Should be false, as the existing thumbnail is good enough (i.e. don't
121   // need to replace the existing thumbnail which is new and good).
122   EXPECT_FALSE(thumbnail_service->ShouldAcquirePageThumbnail(kGoodURL));
123 }