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/ResourcePtr.h"
39 #include "core/loader/DocumentLoader.h"
40 #include "core/testing/DummyPageHolder.h"
41 #include "core/testing/UnitTestHelpers.h"
42 #include "platform/SharedBuffer.h"
43 #include "public/platform/Platform.h"
44 #include "public/platform/WebURL.h"
45 #include "public/platform/WebURLResponse.h"
46 #include "public/platform/WebUnitTestSupport.h"
48 using namespace WebCore;
52 TEST(ImageResourceTest, MultipartImage)
54 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest());
55 cachedImage->setLoading(true);
57 MockImageResourceClient client;
58 cachedImage->addClient(&client);
60 // Send the multipart response. No image or data buffer is created.
61 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, String()));
62 ASSERT_FALSE(cachedImage->resourceBuffer());
63 ASSERT_FALSE(cachedImage->hasImage());
64 ASSERT_EQ(client.imageChangedCount(), 0);
65 ASSERT_FALSE(client.notifyFinishedCalled());
67 // Send the response for the first real part. No image or data buffer is created.
68 const char* svgData = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>";
69 unsigned svgDataLength = strlen(svgData);
70 cachedImage->responseReceived(ResourceResponse(KURL(), "image/svg+xml", svgDataLength, nullAtom, String()));
71 ASSERT_FALSE(cachedImage->resourceBuffer());
72 ASSERT_FALSE(cachedImage->hasImage());
73 ASSERT_EQ(client.imageChangedCount(), 0);
74 ASSERT_FALSE(client.notifyFinishedCalled());
76 // The first bytes arrive. The data buffer is created, but no image is created.
77 cachedImage->appendData(svgData, svgDataLength);
78 ASSERT_TRUE(cachedImage->resourceBuffer());
79 ASSERT_EQ(cachedImage->resourceBuffer()->size(), svgDataLength);
80 ASSERT_FALSE(cachedImage->hasImage());
81 ASSERT_EQ(client.imageChangedCount(), 0);
82 ASSERT_FALSE(client.notifyFinishedCalled());
84 // This part finishes. The image is created, callbacks are sent, and the data buffer is cleared.
85 cachedImage->finish();
86 ASSERT_FALSE(cachedImage->resourceBuffer());
87 ASSERT_FALSE(cachedImage->errorOccurred());
88 ASSERT_TRUE(cachedImage->hasImage());
89 ASSERT_FALSE(cachedImage->image()->isNull());
90 ASSERT_EQ(cachedImage->image()->width(), 1);
91 ASSERT_EQ(cachedImage->image()->height(), 1);
92 ASSERT_EQ(client.imageChangedCount(), 2);
93 ASSERT_TRUE(client.notifyFinishedCalled());
96 TEST(ImageResourceTest, CancelOnDetach)
98 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
100 blink::WebURLResponse response;
101 response.initialize();
102 response.setMIMEType("text/html");
103 WTF::String localPath = String(blink::Platform::current()->unitTestSupport()->webKitRootDir()) + "/Source/web/tests/data/cancelTest.html";
104 blink::Platform::current()->unitTestSupport()->registerMockedURL(testURL, response, localPath);
106 // Create enough of a mocked world to get a functioning ResourceLoader.
107 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create();
108 RefPtr<DocumentLoader> documentLoader = DocumentLoader::create(&dummyPageHolder->frame(), ResourceRequest(testURL), SubstituteData());
110 // Emulate starting a real load.
111 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(testURL));
112 cachedImage->load(documentLoader->fetcher(), ResourceLoaderOptions());
113 memoryCache()->add(cachedImage.get());
115 MockImageResourceClient client;
116 cachedImage->addClient(&client);
117 EXPECT_EQ(Resource::Pending, cachedImage->status());
119 // The load should still be alive, but a timer should be started to cancel the load inside removeClient().
120 cachedImage->removeClient(&client);
121 EXPECT_EQ(Resource::Pending, cachedImage->status());
122 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
124 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
125 WebCore::testing::runPendingTasks();
126 EXPECT_EQ(Resource::LoadError, cachedImage->status());
127 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
129 blink::Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
132 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients)
134 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest());
135 cachedImage->setLoading(true);
137 MockImageResourceClient client;
138 cachedImage->addClient(&client);
140 // Send the image response.
141 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, String()));
142 static const unsigned char jpegData[] = {
143 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00,
144 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
145 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43,
146 0x00, 0x05, 0x03, 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06,
147 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, 0x11, 0x0f, 0x12,
148 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, 0x13, 0x14, 0x1a, 0x15, 0x11, 0x11,
149 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22, 0x24, 0x22, 0x1e,
150 0x24, 0x1c, 0x1e, 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05, 0x05, 0x07, 0x06,
151 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
152 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
153 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
154 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff,
155 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01,
156 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14,
158 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11,
161 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f,
163 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9
166 unsigned jpegDataLength = sizeof(jpegData);
167 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpegDataLength, nullAtom, String()));
168 cachedImage->appendData(reinterpret_cast<const char*>(jpegData), jpegDataLength);
169 cachedImage->finish();
170 ASSERT_FALSE(cachedImage->errorOccurred());
171 ASSERT_TRUE(cachedImage->hasImage());
172 ASSERT_FALSE(cachedImage->image()->isNull());
173 ASSERT_TRUE(client.notifyFinishedCalled());
175 // The prune comes when the ImageResource still has clients. The image should not be deleted.
176 cachedImage->prune();
177 ASSERT_TRUE(cachedImage->hasClients());
178 ASSERT_TRUE(cachedImage->hasImage());
179 ASSERT_FALSE(cachedImage->image()->isNull());
181 // The ImageResource no longer has clients. The image should be deleted by prune.
182 cachedImage->removeClient(&client);
183 cachedImage->prune();
184 ASSERT_FALSE(cachedImage->hasClients());
185 ASSERT_FALSE(cachedImage->hasImage());
186 ASSERT_TRUE(cachedImage->image()->isNull());