2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/fetch/ImageResource.h"
34 #include "core/fetch/ImageResourceClient.h"
35 #include "core/fetch/MemoryCache.h"
36 #include "core/fetch/MockImageResourceClient.h"
37 #include "core/fetch/ResourceFetcher.h"
38 #include "core/fetch/ResourceLoader.h"
39 #include "core/fetch/ResourcePtr.h"
40 #include "core/fetch/UniqueIdentifier.h"
41 #include "platform/SharedBuffer.h"
42 #include "platform/exported/WrappedResourceResponse.h"
43 #include "platform/graphics/Image.h"
44 #include "platform/testing/URLTestHelpers.h"
45 #include "platform/testing/UnitTestHelpers.h"
46 #include "public/platform/Platform.h"
47 #include "public/platform/WebURL.h"
48 #include "public/platform/WebURLResponse.h"
49 #include "public/platform/WebUnitTestSupport.h"
51 #include <gtest/gtest.h>
55 static Vector
<unsigned char> jpegImage()
57 Vector
<unsigned char> jpeg
;
59 static const unsigned char data
[] = {
60 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00,
61 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
62 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43,
63 0x00, 0x05, 0x03, 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06,
64 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, 0x11, 0x0f, 0x12,
65 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, 0x13, 0x14, 0x1a, 0x15, 0x11, 0x11,
66 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22, 0x24, 0x22, 0x1e,
67 0x24, 0x1c, 0x1e, 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05, 0x05, 0x07, 0x06,
68 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
69 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
70 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
71 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff,
72 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01,
73 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14,
75 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11,
78 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f,
80 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9
83 jpeg
.append(data
, sizeof(data
));
87 TEST(ImageResourceTest
, MultipartImage
)
89 ResourceFetcher
* fetcher
= ResourceFetcher::create(nullptr);
90 KURL
testURL(ParsedURLString
, "http://www.test.com/cancelTest.html");
91 URLTestHelpers::registerMockedURLLoad(testURL
, "cancelTest.html", "text/html");
93 // Emulate starting a real load, but don't expect any "real" WebURLLoaderClient callbacks.
94 ResourcePtr
<ImageResource
> cachedImage
= new ImageResource(ResourceRequest(testURL
), nullptr);
95 cachedImage
->setIdentifier(createUniqueIdentifier());
96 cachedImage
->load(fetcher
, ResourceLoaderOptions());
97 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL
);
99 MockImageResourceClient
client(cachedImage
);
100 EXPECT_EQ(Resource::Pending
, cachedImage
->status());
102 // Send the multipart response. No image or data buffer is created.
103 // Note that the response must be routed through ResourceLoader to
104 // ensure the load is flagged as multipart.
105 ResourceResponse
multipartResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom
, String());
106 cachedImage
->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(multipartResponse
), nullptr);
107 ASSERT_FALSE(cachedImage
->resourceBuffer());
108 ASSERT_FALSE(cachedImage
->hasImage());
109 ASSERT_EQ(client
.imageChangedCount(), 0);
110 ASSERT_FALSE(client
.notifyFinishedCalled());
112 // Send the response for the first real part. No image or data buffer is created.
113 const char* svgData
= "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>";
114 unsigned svgDataLength
= strlen(svgData
);
115 ResourceResponse
payloadResponse(KURL(), "image/svg+xml", svgDataLength
, nullAtom
, String());
116 cachedImage
->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(payloadResponse
), nullptr);
117 ASSERT_FALSE(cachedImage
->resourceBuffer());
118 ASSERT_FALSE(cachedImage
->hasImage());
119 ASSERT_EQ(client
.imageChangedCount(), 0);
120 ASSERT_FALSE(client
.notifyFinishedCalled());
122 // The first bytes arrive. The data buffer is created, but no image is created.
123 cachedImage
->appendData(svgData
, svgDataLength
);
124 ASSERT_TRUE(cachedImage
->resourceBuffer());
125 ASSERT_EQ(cachedImage
->resourceBuffer()->size(), svgDataLength
);
126 ASSERT_FALSE(cachedImage
->hasImage());
127 ASSERT_EQ(client
.imageChangedCount(), 0);
128 ASSERT_FALSE(client
.notifyFinishedCalled());
130 // This part finishes. The image is created, callbacks are sent, and the data buffer is cleared.
131 cachedImage
->finish();
132 ASSERT_FALSE(cachedImage
->resourceBuffer());
133 ASSERT_FALSE(cachedImage
->errorOccurred());
134 ASSERT_TRUE(cachedImage
->hasImage());
135 ASSERT_FALSE(cachedImage
->image()->isNull());
136 ASSERT_EQ(cachedImage
->image()->width(), 1);
137 ASSERT_EQ(cachedImage
->image()->height(), 1);
138 ASSERT_EQ(client
.imageChangedCount(), 2);
139 ASSERT_TRUE(client
.notifyFinishedCalled());
142 TEST(ImageResourceTest
, CancelOnDetach
)
144 KURL
testURL(ParsedURLString
, "http://www.test.com/cancelTest.html");
145 URLTestHelpers::registerMockedURLLoad(testURL
, "cancelTest.html", "text/html");
147 ResourceFetcher
* fetcher
= ResourceFetcher::create(nullptr);
149 // Emulate starting a real load.
150 ResourcePtr
<ImageResource
> cachedImage
= new ImageResource(ResourceRequest(testURL
), nullptr);
151 cachedImage
->setIdentifier(createUniqueIdentifier());
153 cachedImage
->load(fetcher
, ResourceLoaderOptions());
154 memoryCache()->add(cachedImage
.get());
156 MockImageResourceClient
client(cachedImage
);
157 EXPECT_EQ(Resource::Pending
, cachedImage
->status());
159 // The load should still be alive, but a timer should be started to cancel the load inside removeClient().
160 client
.removeAsClient();
161 EXPECT_EQ(Resource::Pending
, cachedImage
->status());
162 EXPECT_NE(reinterpret_cast<Resource
*>(0), memoryCache()->resourceForURL(testURL
));
164 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
165 blink::testing::runPendingTasks();
166 EXPECT_EQ(Resource::LoadError
, cachedImage
->status());
167 EXPECT_EQ(reinterpret_cast<Resource
*>(0), memoryCache()->resourceForURL(testURL
));
169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL
);
172 TEST(ImageResourceTest
, DecodedDataRemainsWhileHasClients
)
174 ResourcePtr
<ImageResource
> cachedImage
= new ImageResource(ResourceRequest(), nullptr);
175 cachedImage
->setLoading(true);
177 MockImageResourceClient
client(cachedImage
);
179 // Send the image response.
180 cachedImage
->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom
, String()), nullptr);
182 Vector
<unsigned char> jpeg
= jpegImage();
183 cachedImage
->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg
.size(), nullAtom
, String()), nullptr);
184 cachedImage
->appendData(reinterpret_cast<const char*>(jpeg
.data()), jpeg
.size());
185 cachedImage
->finish();
186 ASSERT_FALSE(cachedImage
->errorOccurred());
187 ASSERT_TRUE(cachedImage
->hasImage());
188 ASSERT_FALSE(cachedImage
->image()->isNull());
189 ASSERT_TRUE(client
.notifyFinishedCalled());
191 // The prune comes when the ImageResource still has clients. The image should not be deleted.
192 cachedImage
->prune();
193 ASSERT_TRUE(cachedImage
->hasClients());
194 ASSERT_TRUE(cachedImage
->hasImage());
195 ASSERT_FALSE(cachedImage
->image()->isNull());
197 // The ImageResource no longer has clients. The image should be deleted by prune.
198 client
.removeAsClient();
199 cachedImage
->prune();
200 ASSERT_FALSE(cachedImage
->hasClients());
201 ASSERT_FALSE(cachedImage
->hasImage());
202 ASSERT_TRUE(cachedImage
->image()->isNull());
205 TEST(ImageResourceTest
, UpdateBitmapImages
)
207 ResourcePtr
<ImageResource
> cachedImage
= new ImageResource(ResourceRequest(), nullptr);
208 cachedImage
->setLoading(true);
210 MockImageResourceClient
client(cachedImage
);
212 // Send the image response.
213 Vector
<unsigned char> jpeg
= jpegImage();
214 cachedImage
->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg
.size(), nullAtom
, String()), nullptr);
215 cachedImage
->appendData(reinterpret_cast<const char*>(jpeg
.data()), jpeg
.size());
216 cachedImage
->finish();
217 ASSERT_FALSE(cachedImage
->errorOccurred());
218 ASSERT_TRUE(cachedImage
->hasImage());
219 ASSERT_FALSE(cachedImage
->image()->isNull());
220 ASSERT_EQ(client
.imageChangedCount(), 2);
221 ASSERT_TRUE(client
.notifyFinishedCalled());
223 HashSet
<ImageResource
*> bitmapImages
;
224 ASSERT_TRUE(cachedImage
->image()->isBitmapImage());
225 bitmapImages
.add(cachedImage
.get());