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(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());
92 scoped_ptr
<BitmapFetcherService
> service_
;
95 int requestsFinished_
;
101 TestingProfile profile_
;
104 TEST_F(BitmapFetcherServiceTest
, RequestInvalidUrl
) {
105 const BitmapFetcherService::RequestId invalid_request_id
=
106 BitmapFetcherService::REQUEST_ID_INVALID
;
108 ASSERT_FALSE(invalid_url
.is_valid());
110 BitmapFetcherService::RequestId request_id
=
111 service_
->RequestImage(invalid_url
, new TestObserver(this));
112 EXPECT_EQ(invalid_request_id
, request_id
);
115 TEST_F(BitmapFetcherServiceTest
, CancelInvalidRequest
) {
116 service_
->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID
);
117 service_
->CancelRequest(23);
120 TEST_F(BitmapFetcherServiceTest
, OnlyFirstRequestCreatesFetcher
) {
121 EXPECT_EQ(0U, active_fetchers().size());
123 service_
->RequestImage(url1_
, new TestObserver(this));
124 EXPECT_EQ(1U, active_fetchers().size());
126 service_
->RequestImage(url1_
, new TestObserver(this));
127 EXPECT_EQ(1U, active_fetchers().size());
130 TEST_F(BitmapFetcherServiceTest
, CompletedFetchNotifiesAllObservers
) {
131 service_
->RequestImage(url1_
, new TestObserver(this));
132 service_
->RequestImage(url1_
, new TestObserver(this));
133 service_
->RequestImage(url1_
, new TestObserver(this));
134 service_
->RequestImage(url1_
, new TestObserver(this));
135 EXPECT_EQ(1U, active_fetchers().size());
136 EXPECT_EQ(4U, requests().size());
138 CompleteFetch(url1_
);
139 EXPECT_EQ(4, imagesChanged_
);
140 EXPECT_EQ(4, requestsFinished_
);
143 TEST_F(BitmapFetcherServiceTest
, CancelRequest
) {
144 service_
->RequestImage(url1_
, new TestObserver(this));
145 service_
->RequestImage(url1_
, new TestObserver(this));
146 BitmapFetcherService::RequestId requestId
=
147 service_
->RequestImage(url2_
, new TestObserver(this));
148 service_
->RequestImage(url1_
, new TestObserver(this));
149 service_
->RequestImage(url1_
, new TestObserver(this));
150 EXPECT_EQ(5U, requests().size());
152 service_
->CancelRequest(requestId
);
153 EXPECT_EQ(4U, requests().size());
155 CompleteFetch(url2_
);
156 EXPECT_EQ(0, imagesChanged_
);
158 CompleteFetch(url1_
);
159 EXPECT_EQ(4, imagesChanged_
);
162 TEST_F(BitmapFetcherServiceTest
, FailedRequestsDontEnterCache
) {
163 service_
->RequestImage(url1_
, new TestObserver(this));
164 service_
->RequestImage(url2_
, new TestObserver(this));
165 EXPECT_EQ(0U, cache_size());
167 CompleteFetch(url1_
);
168 EXPECT_EQ(1U, cache_size());
171 EXPECT_EQ(1U, cache_size());