Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher / bitmap_fetcher_service_unittest.cc
blobe1a162f0f0cf29276fecbc57398f20be2ee97b2e
1 // Copyright 2014 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/bitmap_fetcher/bitmap_fetcher_service.h"
7 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 class TestNotificationInterface {
14 public:
15 virtual ~TestNotificationInterface() {}
16 virtual void OnImageChanged() = 0;
17 virtual void OnRequestFinished() = 0;
20 class TestObserver : public BitmapFetcherService::Observer {
21 public:
22 explicit TestObserver(TestNotificationInterface* target) : target_(target) {}
23 virtual ~TestObserver() { target_->OnRequestFinished(); }
25 virtual void OnImageChanged(BitmapFetcherService::RequestId request_id,
26 const SkBitmap& answers_image) OVERRIDE {
27 target_->OnImageChanged();
30 TestNotificationInterface* target_;
33 class TestService : public BitmapFetcherService {
34 public:
35 explicit TestService(content::BrowserContext* context)
36 : BitmapFetcherService(context) {}
37 virtual ~TestService() {}
39 // Create a fetcher, but don't start downloading. That allows side-stepping
40 // the decode step, which requires a utility process.
41 virtual chrome::BitmapFetcher* CreateFetcher(const GURL& url) OVERRIDE {
42 return new chrome::BitmapFetcher(url, this);
46 } // namespace
48 class BitmapFetcherServiceTest : public testing::Test,
49 public TestNotificationInterface {
50 public:
51 virtual void SetUp() OVERRIDE {
52 service_.reset(new TestService(&profile_));
53 requestsFinished_ = 0;
54 imagesChanged_ = 0;
55 url1_ = GURL("http://example.org/sample-image-1.png");
56 url2_ = GURL("http://example.org/sample-image-2.png");
59 const ScopedVector<BitmapFetcherRequest>& requests() {
60 return service_->requests_;
62 const ScopedVector<chrome::BitmapFetcher>& active_fetchers() {
63 return service_->active_fetchers_;
65 size_t cache_size() { return service_->cache_.size(); }
67 virtual void OnImageChanged() OVERRIDE { imagesChanged_++; }
69 virtual void OnRequestFinished() OVERRIDE { requestsFinished_++; }
71 // Simulate finishing a URL fetch and decode for the given fetcher.
72 void CompleteFetch(const GURL& url) {
73 const chrome::BitmapFetcher* fetcher = service_->FindFetcherForUrl(url);
74 ASSERT_TRUE(NULL != fetcher);
76 // Create a non-empty bitmap.
77 SkBitmap image;
78 image.allocN32Pixels(2, 2);
79 image.eraseColor(SK_ColorGREEN);
81 const_cast<chrome::BitmapFetcher*>(fetcher)->OnImageDecoded(NULL, image);
84 void FailFetch(const GURL& url) {
85 const chrome::BitmapFetcher* fetcher = service_->FindFetcherForUrl(url);
86 ASSERT_TRUE(NULL != fetcher);
87 const_cast<chrome::BitmapFetcher*>(fetcher)
88 ->OnImageDecoded(NULL, SkBitmap());
91 protected:
92 scoped_ptr<BitmapFetcherService> service_;
94 int imagesChanged_;
95 int requestsFinished_;
97 GURL url1_;
98 GURL url2_;
100 private:
101 TestingProfile profile_;
104 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) {
105 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID);
106 service_->CancelRequest(23);
109 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) {
110 EXPECT_EQ(0U, active_fetchers().size());
112 service_->RequestImage(url1_, new TestObserver(this));
113 EXPECT_EQ(1U, active_fetchers().size());
115 service_->RequestImage(url1_, new TestObserver(this));
116 EXPECT_EQ(1U, active_fetchers().size());
119 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) {
120 service_->RequestImage(url1_, new TestObserver(this));
121 service_->RequestImage(url1_, new TestObserver(this));
122 service_->RequestImage(url1_, new TestObserver(this));
123 service_->RequestImage(url1_, new TestObserver(this));
124 EXPECT_EQ(1U, active_fetchers().size());
125 EXPECT_EQ(4U, requests().size());
127 CompleteFetch(url1_);
128 EXPECT_EQ(4, imagesChanged_);
129 EXPECT_EQ(4, requestsFinished_);
132 TEST_F(BitmapFetcherServiceTest, CancelRequest) {
133 service_->RequestImage(url1_, new TestObserver(this));
134 service_->RequestImage(url1_, new TestObserver(this));
135 BitmapFetcherService::RequestId requestId =
136 service_->RequestImage(url2_, new TestObserver(this));
137 service_->RequestImage(url1_, new TestObserver(this));
138 service_->RequestImage(url1_, new TestObserver(this));
139 EXPECT_EQ(5U, requests().size());
141 service_->CancelRequest(requestId);
142 EXPECT_EQ(4U, requests().size());
144 CompleteFetch(url2_);
145 EXPECT_EQ(0, imagesChanged_);
147 CompleteFetch(url1_);
148 EXPECT_EQ(4, imagesChanged_);
151 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) {
152 service_->RequestImage(url1_, new TestObserver(this));
153 service_->RequestImage(url2_, new TestObserver(this));
154 EXPECT_EQ(0U, cache_size());
156 CompleteFetch(url1_);
157 EXPECT_EQ(1U, cache_size());
159 FailFetch(url2_);
160 EXPECT_EQ(1U, cache_size());