OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2013, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "core/fetch/ImageResource.h" | |
33 | |
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" | |
50 | |
51 namespace blink { | |
52 | |
53 static Vector<unsigned char> jpegImage() | |
54 { | |
55 Vector<unsigned char> jpeg; | |
56 | |
57 static const unsigned char data[] = { | |
58 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, | |
59 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, | |
60 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff,
0xdb, 0x00, 0x43, | |
61 0x00, 0x05, 0x03, 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05,
0x05, 0x05, 0x06, | |
62 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c,
0x11, 0x0f, 0x12, | |
63 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, 0x13, 0x14, 0x1a,
0x15, 0x11, 0x11, | |
64 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22,
0x24, 0x22, 0x1e, | |
65 0x24, 0x1c, 0x1e, 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05,
0x05, 0x07, 0x06, | |
66 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
67 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
68 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
69 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0xff, | |
70 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, | |
71 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, | |
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xff,
0xc4, 0x00, 0x14, | |
73 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
74 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4,
0x00, 0x14, 0x11, | |
76 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
77 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
0x11, 0x00, 0x3f, | |
78 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9 | |
79 }; | |
80 | |
81 jpeg.append(data, sizeof(data)); | |
82 return jpeg; | |
83 } | |
84 | |
85 TEST(ImageResourceTest, MultipartImage) | |
86 { | |
87 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); | |
88 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | |
89 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); | |
90 | |
91 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli
ent callbacks. | |
92 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); | |
93 cachedImage->setIdentifier(createUniqueIdentifier()); | |
94 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); | |
95 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | |
96 | |
97 MockImageResourceClient client; | |
98 cachedImage->addClient(&client); | |
99 EXPECT_EQ(Resource::Pending, cachedImage->status()); | |
100 | |
101 // Send the multipart response. No image or data buffer is created. | |
102 // Note that the response must be routed through ResourceLoader to | |
103 // ensure the load is flagged as multipart. | |
104 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, n
ullAtom, String()); | |
105 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(m
ultipartResponse), nullptr); | |
106 ASSERT_FALSE(cachedImage->resourceBuffer()); | |
107 ASSERT_FALSE(cachedImage->hasImage()); | |
108 ASSERT_EQ(client.imageChangedCount(), 0); | |
109 ASSERT_FALSE(client.notifyFinishedCalled()); | |
110 | |
111 // Send the response for the first real part. No image or data buffer is cre
ated. | |
112 const char* svgData = "<svg xmlns='http://www.w3.org/2000/svg' width='1' hei
ght='1'><rect width='1' height='1' fill='green'/></svg>"; | |
113 unsigned svgDataLength = strlen(svgData); | |
114 ResourceResponse payloadResponse(KURL(), "image/svg+xml", svgDataLength, nul
lAtom, String()); | |
115 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(p
ayloadResponse), nullptr); | |
116 ASSERT_FALSE(cachedImage->resourceBuffer()); | |
117 ASSERT_FALSE(cachedImage->hasImage()); | |
118 ASSERT_EQ(client.imageChangedCount(), 0); | |
119 ASSERT_FALSE(client.notifyFinishedCalled()); | |
120 | |
121 // The first bytes arrive. The data buffer is created, but no image is creat
ed. | |
122 cachedImage->appendData(svgData, svgDataLength); | |
123 ASSERT_TRUE(cachedImage->resourceBuffer()); | |
124 ASSERT_EQ(cachedImage->resourceBuffer()->size(), svgDataLength); | |
125 ASSERT_FALSE(cachedImage->hasImage()); | |
126 ASSERT_EQ(client.imageChangedCount(), 0); | |
127 ASSERT_FALSE(client.notifyFinishedCalled()); | |
128 | |
129 // This part finishes. The image is created, callbacks are sent, and the dat
a buffer is cleared. | |
130 cachedImage->finish(); | |
131 ASSERT_FALSE(cachedImage->resourceBuffer()); | |
132 ASSERT_FALSE(cachedImage->errorOccurred()); | |
133 ASSERT_TRUE(cachedImage->hasImage()); | |
134 ASSERT_FALSE(cachedImage->image()->isNull()); | |
135 ASSERT_EQ(cachedImage->image()->width(), 1); | |
136 ASSERT_EQ(cachedImage->image()->height(), 1); | |
137 ASSERT_EQ(client.imageChangedCount(), 2); | |
138 ASSERT_TRUE(client.notifyFinishedCalled()); | |
139 } | |
140 | |
141 TEST(ImageResourceTest, CancelOnDetach) | |
142 { | |
143 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | |
144 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); | |
145 | |
146 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); | |
147 | |
148 // Emulate starting a real load. | |
149 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); | |
150 cachedImage->setIdentifier(createUniqueIdentifier()); | |
151 | |
152 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); | |
153 memoryCache()->add(cachedImage.get()); | |
154 | |
155 MockImageResourceClient client; | |
156 cachedImage->addClient(&client); | |
157 EXPECT_EQ(Resource::Pending, cachedImage->status()); | |
158 | |
159 // The load should still be alive, but a timer should be started to cancel t
he load inside removeClient(). | |
160 cachedImage->removeClient(&client); | |
161 EXPECT_EQ(Resource::Pending, cachedImage->status()); | |
162 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); | |
163 | |
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(test
URL)); | |
168 | |
169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | |
170 } | |
171 | |
172 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) | |
173 { | |
174 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); | |
175 cachedImage->setLoading(true); | |
176 | |
177 MockImageResourceClient client; | |
178 cachedImage->addClient(&client); | |
179 | |
180 // Send the image response. | |
181 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String()), nullptr); | |
182 | |
183 Vector<unsigned char> jpeg = jpegImage(); | |
184 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | |
185 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); | |
186 cachedImage->finish(); | |
187 ASSERT_FALSE(cachedImage->errorOccurred()); | |
188 ASSERT_TRUE(cachedImage->hasImage()); | |
189 ASSERT_FALSE(cachedImage->image()->isNull()); | |
190 ASSERT_TRUE(client.notifyFinishedCalled()); | |
191 | |
192 // The prune comes when the ImageResource still has clients. The image shoul
d not be deleted. | |
193 cachedImage->prune(); | |
194 ASSERT_TRUE(cachedImage->hasClients()); | |
195 ASSERT_TRUE(cachedImage->hasImage()); | |
196 ASSERT_FALSE(cachedImage->image()->isNull()); | |
197 | |
198 // The ImageResource no longer has clients. The image should be deleted by p
rune. | |
199 cachedImage->removeClient(&client); | |
200 cachedImage->prune(); | |
201 ASSERT_FALSE(cachedImage->hasClients()); | |
202 ASSERT_FALSE(cachedImage->hasImage()); | |
203 ASSERT_TRUE(cachedImage->image()->isNull()); | |
204 } | |
205 | |
206 TEST(ImageResourceTest, UpdateBitmapImages) | |
207 { | |
208 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); | |
209 cachedImage->setLoading(true); | |
210 | |
211 MockImageResourceClient client; | |
212 cachedImage->addClient(&client); | |
213 | |
214 // Send the image response. | |
215 Vector<unsigned char> jpeg = jpegImage(); | |
216 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | |
217 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); | |
218 cachedImage->finish(); | |
219 ASSERT_FALSE(cachedImage->errorOccurred()); | |
220 ASSERT_TRUE(cachedImage->hasImage()); | |
221 ASSERT_FALSE(cachedImage->image()->isNull()); | |
222 ASSERT_EQ(client.imageChangedCount(), 2); | |
223 ASSERT_TRUE(client.notifyFinishedCalled()); | |
224 | |
225 HashSet<ImageResource*> bitmapImages; | |
226 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); | |
227 bitmapImages.add(cachedImage.get()); | |
228 } | |
229 | |
230 } // namespace blink | |
OLD | NEW |