| OLD | NEW |
| 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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 std::unique_ptr<scheduler::FakeWebTaskRunner> m_runner; | 193 std::unique_ptr<scheduler::FakeWebTaskRunner> m_runner; |
| 194 }; | 194 }; |
| 195 | 195 |
| 196 TEST(ImageResourceTest, MultipartImage) { | 196 TEST(ImageResourceTest, MultipartImage) { |
| 197 ResourceFetcher* fetcher = | 197 ResourceFetcher* fetcher = |
| 198 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 198 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 199 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 199 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 200 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", | 200 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", |
| 201 "text/html"); | 201 "text/html"); |
| 202 | 202 |
| 203 // Emulate starting a real load, but don't expect any "real" WebURLLoaderClien
t callbacks. | 203 // Emulate starting a real load, but don't expect any "real" |
| 204 // WebURLLoaderClient callbacks. |
| 204 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); | 205 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); |
| 205 cachedImage->setIdentifier(createUniqueIdentifier()); | 206 cachedImage->setIdentifier(createUniqueIdentifier()); |
| 206 fetcher->startLoad(cachedImage); | 207 fetcher->startLoad(cachedImage); |
| 207 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); | 208 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); |
| 208 | 209 |
| 209 Persistent<MockImageResourceClient> client = | 210 Persistent<MockImageResourceClient> client = |
| 210 new MockImageResourceClient(cachedImage); | 211 new MockImageResourceClient(cachedImage); |
| 211 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); | 212 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 212 | 213 |
| 213 // Send the multipart response. No image or data buffer is created. | 214 // Send the multipart response. No image or data buffer is created. Note that |
| 214 // Note that the response must be routed through ResourceLoader to | 215 // the response must be routed through ResourceLoader to ensure the load is |
| 215 // ensure the load is flagged as multipart. | 216 // flagged as multipart. |
| 216 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, | 217 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, |
| 217 nullAtom, String()); | 218 nullAtom, String()); |
| 218 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); | 219 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); |
| 219 cachedImage->loader()->didReceiveResponse( | 220 cachedImage->loader()->didReceiveResponse( |
| 220 nullptr, WrappedResourceResponse(multipartResponse), nullptr); | 221 nullptr, WrappedResourceResponse(multipartResponse), nullptr); |
| 221 EXPECT_FALSE(cachedImage->resourceBuffer()); | 222 EXPECT_FALSE(cachedImage->resourceBuffer()); |
| 222 EXPECT_FALSE(cachedImage->hasImage()); | 223 EXPECT_FALSE(cachedImage->hasImage()); |
| 223 EXPECT_EQ(0, client->imageChangedCount()); | 224 EXPECT_EQ(0, client->imageChangedCount()); |
| 224 EXPECT_FALSE(client->notifyFinishedCalled()); | 225 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 225 EXPECT_EQ("multipart/x-mixed-replace", cachedImage->response().mimeType()); | 226 EXPECT_EQ("multipart/x-mixed-replace", cachedImage->response().mimeType()); |
| 226 | 227 |
| 227 const char firstPart[] = | 228 const char firstPart[] = |
| 228 "--boundary\n" | 229 "--boundary\n" |
| 229 "Content-Type: image/svg+xml\n\n"; | 230 "Content-Type: image/svg+xml\n\n"; |
| 230 cachedImage->appendData(firstPart, strlen(firstPart)); | 231 cachedImage->appendData(firstPart, strlen(firstPart)); |
| 231 // Send the response for the first real part. No image or data buffer is creat
ed. | 232 // Send the response for the first real part. No image or data buffer is |
| 233 // created. |
| 232 EXPECT_FALSE(cachedImage->resourceBuffer()); | 234 EXPECT_FALSE(cachedImage->resourceBuffer()); |
| 233 EXPECT_FALSE(cachedImage->hasImage()); | 235 EXPECT_FALSE(cachedImage->hasImage()); |
| 234 EXPECT_EQ(0, client->imageChangedCount()); | 236 EXPECT_EQ(0, client->imageChangedCount()); |
| 235 EXPECT_FALSE(client->notifyFinishedCalled()); | 237 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 236 EXPECT_EQ("image/svg+xml", cachedImage->response().mimeType()); | 238 EXPECT_EQ("image/svg+xml", cachedImage->response().mimeType()); |
| 237 | 239 |
| 238 const char secondPart[] = | 240 const char secondPart[] = |
| 239 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect " | 241 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect " |
| 240 "width='1' height='1' fill='green'/></svg>\n"; | 242 "width='1' height='1' fill='green'/></svg>\n"; |
| 241 // The first bytes arrive. The data buffer is created, but no image is created
. | 243 // The first bytes arrive. The data buffer is created, but no image is |
| 244 // created. |
| 242 cachedImage->appendData(secondPart, strlen(secondPart)); | 245 cachedImage->appendData(secondPart, strlen(secondPart)); |
| 243 EXPECT_TRUE(cachedImage->resourceBuffer()); | 246 EXPECT_TRUE(cachedImage->resourceBuffer()); |
| 244 EXPECT_FALSE(cachedImage->hasImage()); | 247 EXPECT_FALSE(cachedImage->hasImage()); |
| 245 EXPECT_EQ(0, client->imageChangedCount()); | 248 EXPECT_EQ(0, client->imageChangedCount()); |
| 246 EXPECT_FALSE(client->notifyFinishedCalled()); | 249 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 247 | 250 |
| 248 // Add a client to check an assertion error doesn't happen | 251 // Add a client to check an assertion error doesn't happen |
| 249 // (crbug.com/630983). | 252 // (crbug.com/630983). |
| 250 Persistent<MockImageResourceClient> client2 = | 253 Persistent<MockImageResourceClient> client2 = |
| 251 new MockImageResourceClient(cachedImage); | 254 new MockImageResourceClient(cachedImage); |
| 252 EXPECT_EQ(0, client2->imageChangedCount()); | 255 EXPECT_EQ(0, client2->imageChangedCount()); |
| 253 EXPECT_FALSE(client2->notifyFinishedCalled()); | 256 EXPECT_FALSE(client2->notifyFinishedCalled()); |
| 254 | 257 |
| 255 const char thirdPart[] = "--boundary"; | 258 const char thirdPart[] = "--boundary"; |
| 256 cachedImage->appendData(thirdPart, strlen(thirdPart)); | 259 cachedImage->appendData(thirdPart, strlen(thirdPart)); |
| 257 ASSERT_TRUE(cachedImage->resourceBuffer()); | 260 ASSERT_TRUE(cachedImage->resourceBuffer()); |
| 258 EXPECT_EQ(strlen(secondPart) - 1, cachedImage->resourceBuffer()->size()); | 261 EXPECT_EQ(strlen(secondPart) - 1, cachedImage->resourceBuffer()->size()); |
| 259 | 262 |
| 260 // This part finishes. The image is created, callbacks are sent, and the data
buffer is cleared. | 263 // This part finishes. The image is created, callbacks are sent, and the data |
| 264 // buffer is cleared. |
| 261 cachedImage->loader()->didFinishLoading(nullptr, 0.0, 0); | 265 cachedImage->loader()->didFinishLoading(nullptr, 0.0, 0); |
| 262 EXPECT_TRUE(cachedImage->resourceBuffer()); | 266 EXPECT_TRUE(cachedImage->resourceBuffer()); |
| 263 EXPECT_FALSE(cachedImage->errorOccurred()); | 267 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 264 ASSERT_TRUE(cachedImage->hasImage()); | 268 ASSERT_TRUE(cachedImage->hasImage()); |
| 265 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 269 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 266 EXPECT_EQ(1, cachedImage->getImage()->width()); | 270 EXPECT_EQ(1, cachedImage->getImage()->width()); |
| 267 EXPECT_EQ(1, cachedImage->getImage()->height()); | 271 EXPECT_EQ(1, cachedImage->getImage()->height()); |
| 268 EXPECT_EQ(1, client->imageChangedCount()); | 272 EXPECT_EQ(1, client->imageChangedCount()); |
| 269 EXPECT_TRUE(client->notifyFinishedCalled()); | 273 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 270 EXPECT_EQ(1, client2->imageChangedCount()); | 274 EXPECT_EQ(1, client2->imageChangedCount()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 283 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); | 287 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); |
| 284 cachedImage->setIdentifier(createUniqueIdentifier()); | 288 cachedImage->setIdentifier(createUniqueIdentifier()); |
| 285 | 289 |
| 286 fetcher->startLoad(cachedImage); | 290 fetcher->startLoad(cachedImage); |
| 287 memoryCache()->add(cachedImage); | 291 memoryCache()->add(cachedImage); |
| 288 | 292 |
| 289 Persistent<MockImageResourceClient> client = | 293 Persistent<MockImageResourceClient> client = |
| 290 new MockImageResourceClient(cachedImage); | 294 new MockImageResourceClient(cachedImage); |
| 291 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); | 295 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 292 | 296 |
| 293 // The load should still be alive, but a timer should be started to cancel the
load inside removeClient(). | 297 // The load should still be alive, but a timer should be started to cancel the |
| 298 // load inside removeClient(). |
| 294 client->removeAsClient(); | 299 client->removeAsClient(); |
| 295 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); | 300 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 296 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); | 301 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); |
| 297 | 302 |
| 298 // Trigger the cancel timer, ensure the load was cancelled and the resource wa
s evicted from the cache. | 303 // Trigger the cancel timer, ensure the load was cancelled and the resource |
| 304 // was evicted from the cache. |
| 299 blink::testing::runPendingTasks(); | 305 blink::testing::runPendingTasks(); |
| 300 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); | 306 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); |
| 301 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); | 307 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); |
| 302 | 308 |
| 303 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); | 309 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); |
| 304 } | 310 } |
| 305 | 311 |
| 306 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { | 312 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { |
| 307 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); | 313 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); |
| 308 cachedImage->setStatus(Resource::Pending); | 314 cachedImage->setStatus(Resource::Pending); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 321 ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), | 327 ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), |
| 322 nullptr); | 328 nullptr); |
| 323 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 329 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), |
| 324 jpeg.size()); | 330 jpeg.size()); |
| 325 cachedImage->finish(); | 331 cachedImage->finish(); |
| 326 EXPECT_FALSE(cachedImage->errorOccurred()); | 332 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 327 ASSERT_TRUE(cachedImage->hasImage()); | 333 ASSERT_TRUE(cachedImage->hasImage()); |
| 328 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 334 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 329 EXPECT_TRUE(client->notifyFinishedCalled()); | 335 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 330 | 336 |
| 331 // The prune comes when the ImageResource still has clients. The image should
not be deleted. | 337 // The prune comes when the ImageResource still has clients. The image should |
| 338 // not be deleted. |
| 332 cachedImage->prune(); | 339 cachedImage->prune(); |
| 333 EXPECT_TRUE(cachedImage->isAlive()); | 340 EXPECT_TRUE(cachedImage->isAlive()); |
| 334 ASSERT_TRUE(cachedImage->hasImage()); | 341 ASSERT_TRUE(cachedImage->hasImage()); |
| 335 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 342 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 336 | 343 |
| 337 // The ImageResource no longer has clients. The decoded image data should be | 344 // The ImageResource no longer has clients. The decoded image data should be |
| 338 // deleted by prune. | 345 // deleted by prune. |
| 339 client->removeAsClient(); | 346 client->removeAsClient(); |
| 340 cachedImage->prune(); | 347 cachedImage->prune(); |
| 341 EXPECT_FALSE(cachedImage->isAlive()); | 348 EXPECT_FALSE(cachedImage->isAlive()); |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 nullptr, WrappedResourceResponse(ResourceResponse( | 685 nullptr, WrappedResourceResponse(ResourceResponse( |
| 679 testURL, "image/jpeg", 18, nullAtom, String())), | 686 testURL, "image/jpeg", 18, nullAtom, String())), |
| 680 nullptr); | 687 nullptr); |
| 681 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, | 688 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, |
| 682 18); | 689 18); |
| 683 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); | 690 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); |
| 684 EXPECT_FALSE(cachedImage->isLoading()); | 691 EXPECT_FALSE(cachedImage->isLoading()); |
| 685 } | 692 } |
| 686 | 693 |
| 687 } // namespace blink | 694 } // namespace blink |
| OLD | NEW |