Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp

Issue 1998073002: Move some ResourceClients for testing to Oilpan heap (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved. 2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create()); 119 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create());
120 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 120 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
121 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 121 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
122 122
123 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli ent callbacks. 123 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli ent callbacks.
124 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ; 124 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ;
125 cachedImage->setIdentifier(createUniqueIdentifier()); 125 cachedImage->setIdentifier(createUniqueIdentifier());
126 cachedImage->load(fetcher); 126 cachedImage->load(fetcher);
127 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); 127 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL);
128 128
129 MockImageResourceClient client(cachedImage); 129 Persistent<MockImageResourceClient> client = new MockImageResourceClient(cac hedImage);
130 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 130 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
131 131
132 // Send the multipart response. No image or data buffer is created. 132 // Send the multipart response. No image or data buffer is created.
133 // Note that the response must be routed through ResourceLoader to 133 // Note that the response must be routed through ResourceLoader to
134 // ensure the load is flagged as multipart. 134 // ensure the load is flagged as multipart.
135 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, n ullAtom, String()); 135 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, n ullAtom, String());
136 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); 136 multipartResponse.setMultipartBoundary("boundary", strlen("boundary"));
137 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(m ultipartResponse), nullptr); 137 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(m ultipartResponse), nullptr);
138 ASSERT_FALSE(cachedImage->resourceBuffer()); 138 ASSERT_FALSE(cachedImage->resourceBuffer());
139 ASSERT_FALSE(cachedImage->hasImage()); 139 ASSERT_FALSE(cachedImage->hasImage());
140 ASSERT_EQ(client.imageChangedCount(), 0); 140 ASSERT_EQ(client->imageChangedCount(), 0);
141 ASSERT_FALSE(client.notifyFinishedCalled()); 141 ASSERT_FALSE(client->notifyFinishedCalled());
142 EXPECT_EQ("multipart/x-mixed-replace", cachedImage->response().mimeType()); 142 EXPECT_EQ("multipart/x-mixed-replace", cachedImage->response().mimeType());
143 143
144 const char firstPart[] = 144 const char firstPart[] =
145 "--boundary\n" 145 "--boundary\n"
146 "Content-Type: image/svg+xml\n\n"; 146 "Content-Type: image/svg+xml\n\n";
147 cachedImage->appendData(firstPart, strlen(firstPart)); 147 cachedImage->appendData(firstPart, strlen(firstPart));
148 // Send the response for the first real part. No image or data buffer is cre ated. 148 // Send the response for the first real part. No image or data buffer is cre ated.
149 ASSERT_FALSE(cachedImage->resourceBuffer()); 149 ASSERT_FALSE(cachedImage->resourceBuffer());
150 ASSERT_FALSE(cachedImage->hasImage()); 150 ASSERT_FALSE(cachedImage->hasImage());
151 ASSERT_EQ(client.imageChangedCount(), 0); 151 ASSERT_EQ(client->imageChangedCount(), 0);
152 ASSERT_FALSE(client.notifyFinishedCalled()); 152 ASSERT_FALSE(client->notifyFinishedCalled());
153 EXPECT_EQ("image/svg+xml", cachedImage->response().mimeType()); 153 EXPECT_EQ("image/svg+xml", cachedImage->response().mimeType());
154 154
155 const char secondPart[] = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>\n"; 155 const char secondPart[] = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>\n";
156 // The first bytes arrive. The data buffer is created, but no image is creat ed. 156 // The first bytes arrive. The data buffer is created, but no image is creat ed.
157 cachedImage->appendData(secondPart, strlen(secondPart)); 157 cachedImage->appendData(secondPart, strlen(secondPart));
158 ASSERT_TRUE(cachedImage->resourceBuffer()); 158 ASSERT_TRUE(cachedImage->resourceBuffer());
159 ASSERT_FALSE(cachedImage->hasImage()); 159 ASSERT_FALSE(cachedImage->hasImage());
160 ASSERT_EQ(client.imageChangedCount(), 0); 160 ASSERT_EQ(client->imageChangedCount(), 0);
161 ASSERT_FALSE(client.notifyFinishedCalled()); 161 ASSERT_FALSE(client->notifyFinishedCalled());
162 162
163 const char thirdPart[] = "--boundary"; 163 const char thirdPart[] = "--boundary";
164 cachedImage->appendData(thirdPart, strlen(thirdPart)); 164 cachedImage->appendData(thirdPart, strlen(thirdPart));
165 ASSERT_TRUE(cachedImage->resourceBuffer()); 165 ASSERT_TRUE(cachedImage->resourceBuffer());
166 ASSERT_EQ(cachedImage->resourceBuffer()->size(), strlen(secondPart) - 1); 166 ASSERT_EQ(cachedImage->resourceBuffer()->size(), strlen(secondPart) - 1);
167 167
168 // This part finishes. The image is created, callbacks are sent, and the dat a buffer is cleared. 168 // This part finishes. The image is created, callbacks are sent, and the dat a buffer is cleared.
169 cachedImage->loader()->didFinishLoading(nullptr, 0.0, 0); 169 cachedImage->loader()->didFinishLoading(nullptr, 0.0, 0);
170 ASSERT_FALSE(cachedImage->resourceBuffer()); 170 ASSERT_FALSE(cachedImage->resourceBuffer());
171 ASSERT_FALSE(cachedImage->errorOccurred()); 171 ASSERT_FALSE(cachedImage->errorOccurred());
172 ASSERT_TRUE(cachedImage->hasImage()); 172 ASSERT_TRUE(cachedImage->hasImage());
173 ASSERT_FALSE(cachedImage->getImage()->isNull()); 173 ASSERT_FALSE(cachedImage->getImage()->isNull());
174 ASSERT_EQ(cachedImage->getImage()->width(), 1); 174 ASSERT_EQ(cachedImage->getImage()->width(), 1);
175 ASSERT_EQ(cachedImage->getImage()->height(), 1); 175 ASSERT_EQ(cachedImage->getImage()->height(), 1);
176 ASSERT_EQ(client.imageChangedCount(), 1); 176 ASSERT_EQ(client->imageChangedCount(), 1);
177 ASSERT_TRUE(client.notifyFinishedCalled()); 177 ASSERT_TRUE(client->notifyFinishedCalled());
178 } 178 }
179 179
180 TEST(ImageResourceTest, CancelOnDetach) 180 TEST(ImageResourceTest, CancelOnDetach)
181 { 181 {
182 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 182 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
183 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 183 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
184 184
185 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create()); 185 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create());
186 186
187 // Emulate starting a real load. 187 // Emulate starting a real load.
188 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ; 188 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ;
189 cachedImage->setIdentifier(createUniqueIdentifier()); 189 cachedImage->setIdentifier(createUniqueIdentifier());
190 190
191 cachedImage->load(fetcher); 191 cachedImage->load(fetcher);
192 memoryCache()->add(cachedImage); 192 memoryCache()->add(cachedImage);
193 193
194 MockImageResourceClient client(cachedImage); 194 Persistent<MockImageResourceClient> client = new MockImageResourceClient(cac hedImage);
195 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 195 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
196 196
197 // The load should still be alive, but a timer should be started to cancel t he load inside removeClient(). 197 // The load should still be alive, but a timer should be started to cancel t he load inside removeClient().
198 client.removeAsClient(); 198 client->removeAsClient();
199 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 199 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
200 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL)); 200 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL));
201 201
202 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache. 202 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
203 blink::testing::runPendingTasks(); 203 blink::testing::runPendingTasks();
204 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); 204 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus());
205 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL)); 205 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL));
206 206
207 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); 207 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL);
208 } 208 }
209 209
210 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) 210 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients)
211 { 211 {
212 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 212 ImageResource* cachedImage = ImageResource::create(ResourceRequest());
213 cachedImage->setStatus(Resource::Pending); 213 cachedImage->setStatus(Resource::Pending);
214 214
215 MockImageResourceClient client(cachedImage); 215 Persistent<MockImageResourceClient> client = new MockImageResourceClient(cac hedImage);
216 216
217 // Send the image response. 217 // Send the image response.
218 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, nullAtom, String()), nullptr); 218 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, nullAtom, String()), nullptr);
219 219
220 Vector<unsigned char> jpeg = jpegImage(); 220 Vector<unsigned char> jpeg = jpegImage();
221 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr); 221 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr);
222 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 222 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
223 cachedImage->finish(); 223 cachedImage->finish();
224 ASSERT_FALSE(cachedImage->errorOccurred()); 224 ASSERT_FALSE(cachedImage->errorOccurred());
225 ASSERT_TRUE(cachedImage->hasImage()); 225 ASSERT_TRUE(cachedImage->hasImage());
226 ASSERT_FALSE(cachedImage->getImage()->isNull()); 226 ASSERT_FALSE(cachedImage->getImage()->isNull());
227 ASSERT_TRUE(client.notifyFinishedCalled()); 227 ASSERT_TRUE(client->notifyFinishedCalled());
228 228
229 // The prune comes when the ImageResource still has clients. The image shoul d not be deleted. 229 // The prune comes when the ImageResource still has clients. The image shoul d not be deleted.
230 cachedImage->prune(); 230 cachedImage->prune();
231 ASSERT_TRUE(cachedImage->hasClientsOrObservers()); 231 ASSERT_TRUE(cachedImage->hasClientsOrObservers());
232 ASSERT_TRUE(cachedImage->hasImage()); 232 ASSERT_TRUE(cachedImage->hasImage());
233 ASSERT_FALSE(cachedImage->getImage()->isNull()); 233 ASSERT_FALSE(cachedImage->getImage()->isNull());
234 234
235 // The ImageResource no longer has clients. The image should be deleted by p rune. 235 // The ImageResource no longer has clients. The image should be deleted by p rune.
236 client.removeAsClient(); 236 client->removeAsClient();
237 cachedImage->prune(); 237 cachedImage->prune();
238 ASSERT_FALSE(cachedImage->hasClientsOrObservers()); 238 ASSERT_FALSE(cachedImage->hasClientsOrObservers());
239 ASSERT_FALSE(cachedImage->hasImage()); 239 ASSERT_FALSE(cachedImage->hasImage());
240 ASSERT_TRUE(cachedImage->getImage()->isNull()); 240 ASSERT_TRUE(cachedImage->getImage()->isNull());
241 } 241 }
242 242
243 TEST(ImageResourceTest, UpdateBitmapImages) 243 TEST(ImageResourceTest, UpdateBitmapImages)
244 { 244 {
245 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 245 ImageResource* cachedImage = ImageResource::create(ResourceRequest());
246 cachedImage->setStatus(Resource::Pending); 246 cachedImage->setStatus(Resource::Pending);
247 247
248 MockImageResourceClient client(cachedImage); 248 Persistent<MockImageResourceClient> client = new MockImageResourceClient(cac hedImage);
249 249
250 // Send the image response. 250 // Send the image response.
251 Vector<unsigned char> jpeg = jpegImage(); 251 Vector<unsigned char> jpeg = jpegImage();
252 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr); 252 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr);
253 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 253 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
254 cachedImage->finish(); 254 cachedImage->finish();
255 ASSERT_FALSE(cachedImage->errorOccurred()); 255 ASSERT_FALSE(cachedImage->errorOccurred());
256 ASSERT_TRUE(cachedImage->hasImage()); 256 ASSERT_TRUE(cachedImage->hasImage());
257 ASSERT_FALSE(cachedImage->getImage()->isNull()); 257 ASSERT_FALSE(cachedImage->getImage()->isNull());
258 ASSERT_EQ(client.imageChangedCount(), 2); 258 ASSERT_EQ(client->imageChangedCount(), 2);
259 ASSERT_TRUE(client.notifyFinishedCalled()); 259 ASSERT_TRUE(client->notifyFinishedCalled());
260 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage()); 260 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage());
261 } 261 }
262 262
263 TEST(ImageResourceTest, ReloadIfLoFi) 263 TEST(ImageResourceTest, ReloadIfLoFi)
264 { 264 {
265 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 265 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
266 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 266 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
267 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ; 267 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)) ;
268 cachedImage->setStatus(Resource::Pending); 268 cachedImage->setStatus(Resource::Pending);
269 269
270 MockImageResourceClient client(cachedImage); 270 Persistent<MockImageResourceClient> client = new MockImageResourceClient(cac hedImage);
271 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create()); 271 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc hContext::create());
272 272
273 // Send the image response. 273 // Send the image response.
274 Vector<unsigned char> jpeg = jpegImage(); 274 Vector<unsigned char> jpeg = jpegImage();
275 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAto m, String()); 275 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAto m, String());
276 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 276 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
277 277
278 cachedImage->responseReceived(resourceResponse, nullptr); 278 cachedImage->responseReceived(resourceResponse, nullptr);
279 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 279 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
280 cachedImage->finish(); 280 cachedImage->finish();
281 ASSERT_FALSE(cachedImage->errorOccurred()); 281 ASSERT_FALSE(cachedImage->errorOccurred());
282 ASSERT_TRUE(cachedImage->hasImage()); 282 ASSERT_TRUE(cachedImage->hasImage());
283 ASSERT_FALSE(cachedImage->getImage()->isNull()); 283 ASSERT_FALSE(cachedImage->getImage()->isNull());
284 ASSERT_EQ(client.imageChangedCount(), 2); 284 ASSERT_EQ(client->imageChangedCount(), 2);
285 ASSERT_TRUE(client.notifyFinishedCalled()); 285 ASSERT_TRUE(client->notifyFinishedCalled());
286 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage()); 286 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage());
287 287
288 cachedImage->reloadIfLoFi(fetcher); 288 cachedImage->reloadIfLoFi(fetcher);
289 ASSERT_FALSE(cachedImage->errorOccurred()); 289 ASSERT_FALSE(cachedImage->errorOccurred());
290 ASSERT_FALSE(cachedImage->resourceBuffer()); 290 ASSERT_FALSE(cachedImage->resourceBuffer());
291 ASSERT_TRUE(cachedImage->hasImage()); 291 ASSERT_TRUE(cachedImage->hasImage());
292 ASSERT_EQ(client.imageChangedCount(), 3); 292 ASSERT_EQ(client->imageChangedCount(), 3);
293 293
294 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(r esourceResponse), nullptr); 294 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(r esourceResponse), nullptr);
295 cachedImage->loader()->didReceiveData(nullptr, reinterpret_cast<const char*> (jpeg.data()), jpeg.size(), jpeg.size()); 295 cachedImage->loader()->didReceiveData(nullptr, reinterpret_cast<const char*> (jpeg.data()), jpeg.size(), jpeg.size());
296 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg.size()); 296 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg.size());
297 ASSERT_FALSE(cachedImage->errorOccurred()); 297 ASSERT_FALSE(cachedImage->errorOccurred());
298 ASSERT_TRUE(cachedImage->hasImage()); 298 ASSERT_TRUE(cachedImage->hasImage());
299 ASSERT_FALSE(cachedImage->getImage()->isNull()); 299 ASSERT_FALSE(cachedImage->getImage()->isNull());
300 ASSERT_TRUE(client.notifyFinishedCalled()); 300 ASSERT_TRUE(client->notifyFinishedCalled());
301 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage()); 301 ASSERT_TRUE(cachedImage->getImage()->isBitmapImage());
302 } 302 }
303 303
304 } // namespace blink 304 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698