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