Implement timers by posting delayed tasks
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / ImageResourceTest.cpp
blob72969f803ee648f743870a13249b592327b5e4b5
1 /*
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
6 * met:
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
13 * distribution.
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.
31 #include "config.h"
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 using namespace blink;
53 namespace {
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));
84 return jpeg;
87 TEST(ImageResourceTest, MultipartImage)
89 RefPtrWillBeRawPtr<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));
95 cachedImage->setIdentifier(createUniqueIdentifier());
96 cachedImage->load(fetcher.get(), ResourceLoaderOptions());
97 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
99 MockImageResourceClient client;
100 cachedImage->addClient(&client);
101 EXPECT_EQ(Resource::Pending, cachedImage->status());
103 // Send the multipart response. No image or data buffer is created.
104 // Note that the response must be routed through ResourceLoader to
105 // ensure the load is flagged as multipart.
106 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, String());
107 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(multipartResponse), nullptr);
108 ASSERT_FALSE(cachedImage->resourceBuffer());
109 ASSERT_FALSE(cachedImage->hasImage());
110 ASSERT_EQ(client.imageChangedCount(), 0);
111 ASSERT_FALSE(client.notifyFinishedCalled());
113 // Send the response for the first real part. No image or data buffer is created.
114 const char* svgData = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>";
115 unsigned svgDataLength = strlen(svgData);
116 ResourceResponse payloadResponse(KURL(), "image/svg+xml", svgDataLength, nullAtom, String());
117 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(payloadResponse), nullptr);
118 ASSERT_FALSE(cachedImage->resourceBuffer());
119 ASSERT_FALSE(cachedImage->hasImage());
120 ASSERT_EQ(client.imageChangedCount(), 0);
121 ASSERT_FALSE(client.notifyFinishedCalled());
123 // The first bytes arrive. The data buffer is created, but no image is created.
124 cachedImage->appendData(svgData, svgDataLength);
125 ASSERT_TRUE(cachedImage->resourceBuffer());
126 ASSERT_EQ(cachedImage->resourceBuffer()->size(), svgDataLength);
127 ASSERT_FALSE(cachedImage->hasImage());
128 ASSERT_EQ(client.imageChangedCount(), 0);
129 ASSERT_FALSE(client.notifyFinishedCalled());
131 // This part finishes. The image is created, callbacks are sent, and the data buffer is cleared.
132 cachedImage->finish();
133 ASSERT_FALSE(cachedImage->resourceBuffer());
134 ASSERT_FALSE(cachedImage->errorOccurred());
135 ASSERT_TRUE(cachedImage->hasImage());
136 ASSERT_FALSE(cachedImage->image()->isNull());
137 ASSERT_EQ(cachedImage->image()->width(), 1);
138 ASSERT_EQ(cachedImage->image()->height(), 1);
139 ASSERT_EQ(client.imageChangedCount(), 2);
140 ASSERT_TRUE(client.notifyFinishedCalled());
143 TEST(ImageResourceTest, CancelOnDetach)
145 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
146 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html");
148 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullptr);
150 // Emulate starting a real load.
151 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(testURL));
152 cachedImage->setIdentifier(createUniqueIdentifier());
154 cachedImage->load(fetcher.get(), ResourceLoaderOptions());
155 memoryCache()->add(cachedImage.get());
157 MockImageResourceClient client;
158 cachedImage->addClient(&client);
159 EXPECT_EQ(Resource::Pending, cachedImage->status());
161 // The load should still be alive, but a timer should be started to cancel the load inside removeClient().
162 cachedImage->removeClient(&client);
163 EXPECT_EQ(Resource::Pending, cachedImage->status());
164 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
166 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
167 blink::testing::runPendingTasks();
168 EXPECT_EQ(Resource::LoadError, cachedImage->status());
169 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
171 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
174 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients)
176 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest());
177 cachedImage->setLoading(true);
179 MockImageResourceClient client;
180 cachedImage->addClient(&client);
182 // Send the image response.
183 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, String()), nullptr);
185 Vector<unsigned char> jpeg = jpegImage();
186 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), nullptr);
187 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.size());
188 cachedImage->finish();
189 ASSERT_FALSE(cachedImage->errorOccurred());
190 ASSERT_TRUE(cachedImage->hasImage());
191 ASSERT_FALSE(cachedImage->image()->isNull());
192 ASSERT_TRUE(client.notifyFinishedCalled());
194 // The prune comes when the ImageResource still has clients. The image should not be deleted.
195 cachedImage->prune();
196 ASSERT_TRUE(cachedImage->hasClients());
197 ASSERT_TRUE(cachedImage->hasImage());
198 ASSERT_FALSE(cachedImage->image()->isNull());
200 // The ImageResource no longer has clients. The image should be deleted by prune.
201 cachedImage->removeClient(&client);
202 cachedImage->prune();
203 ASSERT_FALSE(cachedImage->hasClients());
204 ASSERT_FALSE(cachedImage->hasImage());
205 ASSERT_TRUE(cachedImage->image()->isNull());
208 TEST(ImageResourceTest, UpdateBitmapImages)
210 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest());
211 cachedImage->setLoading(true);
213 MockImageResourceClient client;
214 cachedImage->addClient(&client);
216 // Send the image response.
217 Vector<unsigned char> jpeg = jpegImage();
218 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), nullptr);
219 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.size());
220 cachedImage->finish();
221 ASSERT_FALSE(cachedImage->errorOccurred());
222 ASSERT_TRUE(cachedImage->hasImage());
223 ASSERT_FALSE(cachedImage->image()->isNull());
224 ASSERT_EQ(client.imageChangedCount(), 2);
225 ASSERT_TRUE(client.notifyFinishedCalled());
227 HashSet<ImageResource*> bitmapImages;
228 ASSERT_TRUE(cachedImage->image()->isBitmapImage());
229 bitmapImages.add(cachedImage.get());
232 } // namespace