Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher / bitmap_fetcher_service_unittest.cc
blob73513c87ba68e4a9ee3bc3bb1e2aa8c99aecbb1c
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 ~TestObserver() override { target_->OnRequestFinished(); }
25 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 ~TestService() override {}
39 // Create a fetcher, but don't start downloading. That allows side-stepping
40 // the decode step, which requires a utility process.
41 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 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 void OnImageChanged() override { imagesChanged_++; }
69 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(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)->OnImageDecoded(SkBitmap());
90 protected:
91 scoped_ptr<BitmapFetcherService> service_;
93 int imagesChanged_;
94 int requestsFinished_;
96 GURL url1_;
97 GURL url2_;
99 private:
100 TestingProfile profile_;
103 TEST_F(BitmapFetcherServiceTest, RequestInvalidUrl) {
104 const BitmapFetcherService::RequestId invalid_request_id =
105 BitmapFetcherService::REQUEST_ID_INVALID;
106 GURL invalid_url;
107 ASSERT_FALSE(invalid_url.is_valid());
109 BitmapFetcherService::RequestId request_id =
110 service_->RequestImage(invalid_url, new TestObserver(this));
111 EXPECT_EQ(invalid_request_id, request_id);
114 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) {
115 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID);
116 service_->CancelRequest(23);
119 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) {
120 EXPECT_EQ(0U, active_fetchers().size());
122 service_->RequestImage(url1_, new TestObserver(this));
123 EXPECT_EQ(1U, active_fetchers().size());
125 service_->RequestImage(url1_, new TestObserver(this));
126 EXPECT_EQ(1U, active_fetchers().size());
129 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) {
130 service_->RequestImage(url1_, new TestObserver(this));
131 service_->RequestImage(url1_, new TestObserver(this));
132 service_->RequestImage(url1_, new TestObserver(this));
133 service_->RequestImage(url1_, new TestObserver(this));
134 EXPECT_EQ(1U, active_fetchers().size());
135 EXPECT_EQ(4U, requests().size());
137 CompleteFetch(url1_);
138 EXPECT_EQ(4, imagesChanged_);
139 EXPECT_EQ(4, requestsFinished_);
142 TEST_F(BitmapFetcherServiceTest, CancelRequest) {
143 service_->RequestImage(url1_, new TestObserver(this));
144 service_->RequestImage(url1_, new TestObserver(this));
145 BitmapFetcherService::RequestId requestId =
146 service_->RequestImage(url2_, new TestObserver(this));
147 service_->RequestImage(url1_, new TestObserver(this));
148 service_->RequestImage(url1_, new TestObserver(this));
149 EXPECT_EQ(5U, requests().size());
151 service_->CancelRequest(requestId);
152 EXPECT_EQ(4U, requests().size());
154 CompleteFetch(url2_);
155 EXPECT_EQ(0, imagesChanged_);
157 CompleteFetch(url1_);
158 EXPECT_EQ(4, imagesChanged_);
161 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) {
162 service_->RequestImage(url1_, new TestObserver(this));
163 service_->RequestImage(url2_, new TestObserver(this));
164 EXPECT_EQ(0U, cache_size());
166 CompleteFetch(url1_);
167 EXPECT_EQ(1U, cache_size());
169 FailFetch(url2_);
170 EXPECT_EQ(1U, cache_size());