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"
13 class TestNotificationInterface
{
15 virtual ~TestNotificationInterface() {}
16 virtual void OnImageChanged() = 0;
17 virtual void OnRequestFinished() = 0;
20 class TestObserver
: public BitmapFetcherService::Observer
{
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
{
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);
48 class BitmapFetcherServiceTest
: public testing::Test
,
49 public TestNotificationInterface
{
51 void SetUp() override
{
52 service_
.reset(new TestService(&profile_
));
53 requestsFinished_
= 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.
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());
91 scoped_ptr
<BitmapFetcherService
> service_
;
94 int requestsFinished_
;
100 TestingProfile profile_
;
103 TEST_F(BitmapFetcherServiceTest
, RequestInvalidUrl
) {
104 const BitmapFetcherService::RequestId invalid_request_id
=
105 BitmapFetcherService::REQUEST_ID_INVALID
;
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());
170 EXPECT_EQ(1U, cache_size());