| 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 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "core/loader/resource/ImageResource.h" | 31 #include "core/loader/resource/ImageResource.h" |
| 32 | 32 |
| 33 #include "core/fetch/FetchInitiatorInfo.h" | 33 #include "core/fetch/FetchInitiatorInfo.h" |
| 34 #include "core/fetch/FetchRequest.h" | 34 #include "core/fetch/FetchRequest.h" |
| 35 #include "core/fetch/MemoryCache.h" | 35 #include "core/fetch/MemoryCache.h" |
| 36 #include "core/fetch/MockResourceClient.h" | 36 #include "core/fetch/MockResourceClient.h" |
| 37 #include "core/fetch/ResourceFetcher.h" | 37 #include "core/fetch/ResourceFetcher.h" |
| 38 #include "core/fetch/ResourceLoader.h" | 38 #include "core/fetch/ResourceLoader.h" |
| 39 #include "core/fetch/UniqueIdentifier.h" | 39 #include "core/fetch/UniqueIdentifier.h" |
| 40 #include "core/loader/resource/MockImageResourceClient.h" | 40 #include "core/loader/resource/MockImageResourceObserver.h" |
| 41 #include "platform/SharedBuffer.h" | 41 #include "platform/SharedBuffer.h" |
| 42 #include "platform/exported/WrappedResourceResponse.h" | 42 #include "platform/exported/WrappedResourceResponse.h" |
| 43 #include "platform/graphics/BitmapImage.h" | 43 #include "platform/graphics/BitmapImage.h" |
| 44 #include "platform/graphics/Image.h" | 44 #include "platform/graphics/Image.h" |
| 45 #include "platform/scheduler/test/fake_web_task_runner.h" | 45 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 46 #include "platform/testing/TestingPlatformSupport.h" | 46 #include "platform/testing/TestingPlatformSupport.h" |
| 47 #include "platform/testing/URLTestHelpers.h" | 47 #include "platform/testing/URLTestHelpers.h" |
| 48 #include "platform/testing/UnitTestHelpers.h" | 48 #include "platform/testing/UnitTestHelpers.h" |
| 49 #include "public/platform/Platform.h" | 49 #include "public/platform/Platform.h" |
| 50 #include "public/platform/WebCachePolicy.h" | 50 #include "public/platform/WebCachePolicy.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 82 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01, 0x00, | 83 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01, 0x00, |
| 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, | 85 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, |
| 86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 87 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, | 87 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, | 88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, |
| 89 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, | 89 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, |
| 90 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; | 90 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; |
| 91 | 91 |
| 92 const size_t kJpegImageSubrangeWithDimensionsLength = sizeof(kJpegImage) - 1; | 92 constexpr int kJpegImageWidth = 1; |
| 93 |
| 94 constexpr size_t kJpegImageSubrangeWithDimensionsLength = |
| 95 sizeof(kJpegImage) - 1; |
| 93 | 96 |
| 94 // Ensure that the image decoder can determine the dimensions of kJpegImage from | 97 // Ensure that the image decoder can determine the dimensions of kJpegImage from |
| 95 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test | 98 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test |
| 96 // fails, then the test data here probably needs to be updated. | 99 // fails, then the test data here probably needs to be updated. |
| 97 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) { | 100 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) { |
| 98 RefPtr<Image> image = BitmapImage::create(); | 101 RefPtr<Image> image = BitmapImage::create(); |
| 99 EXPECT_EQ( | 102 EXPECT_EQ( |
| 100 Image::SizeAvailable, | 103 Image::SizeAvailable, |
| 101 image->setData(SharedBuffer::create( | 104 image->setData(SharedBuffer::create( |
| 102 kJpegImage, kJpegImageSubrangeWithDimensionsLength), | 105 kJpegImage, kJpegImageSubrangeWithDimensionsLength), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 131 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 129 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, | 132 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, |
| 130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, | 134 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, |
| 132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, | 136 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, |
| 134 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, | 137 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 136 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; | 139 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; |
| 137 | 140 |
| 141 constexpr int kJpegImage2Width = 50; |
| 142 |
| 138 const char kSvgImage[] = | 143 const char kSvgImage[] = |
| 139 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " | 144 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " |
| 140 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | 145 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
| 141 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" | 146 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" |
| 142 "</svg>"; | 147 "</svg>"; |
| 143 | 148 |
| 144 const char kSvgImage2[] = | 149 const char kSvgImage2[] = |
| 145 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " | 150 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " |
| 146 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | 151 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
| 147 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" | 152 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 229 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 225 ScopedRegisteredURL scopedRegisteredURL(testURL); | 230 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 226 | 231 |
| 227 // Emulate starting a real load, but don't expect any "real" | 232 // Emulate starting a real load, but don't expect any "real" |
| 228 // WebURLLoaderClient callbacks. | 233 // WebURLLoaderClient callbacks. |
| 229 ImageResource* imageResource = | 234 ImageResource* imageResource = |
| 230 ImageResource::create(ResourceRequest(testURL)); | 235 ImageResource::create(ResourceRequest(testURL)); |
| 231 imageResource->setIdentifier(createUniqueIdentifier()); | 236 imageResource->setIdentifier(createUniqueIdentifier()); |
| 232 fetcher->startLoad(imageResource); | 237 fetcher->startLoad(imageResource); |
| 233 | 238 |
| 234 Persistent<MockImageResourceClient> client = | 239 std::unique_ptr<MockImageResourceObserver> observer = |
| 235 new MockImageResourceClient(imageResource); | 240 MockImageResourceObserver::create(imageResource->getContent()); |
| 236 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 241 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 237 | 242 |
| 238 // Send the multipart response. No image or data buffer is created. Note that | 243 // Send the multipart response. No image or data buffer is created. Note that |
| 239 // the response must be routed through ResourceLoader to ensure the load is | 244 // the response must be routed through ResourceLoader to ensure the load is |
| 240 // flagged as multipart. | 245 // flagged as multipart. |
| 241 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, | 246 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, |
| 242 nullAtom, String()); | 247 nullAtom, String()); |
| 243 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); | 248 multipartResponse.setMultipartBoundary("boundary", strlen("boundary")); |
| 244 imageResource->loader()->didReceiveResponse( | 249 imageResource->loader()->didReceiveResponse( |
| 245 WrappedResourceResponse(multipartResponse), nullptr); | 250 WrappedResourceResponse(multipartResponse), nullptr); |
| 246 EXPECT_FALSE(imageResource->resourceBuffer()); | 251 EXPECT_FALSE(imageResource->resourceBuffer()); |
| 247 EXPECT_FALSE(imageResource->getContent()->hasImage()); | 252 EXPECT_FALSE(imageResource->getContent()->hasImage()); |
| 248 EXPECT_EQ(0, client->imageChangedCount()); | 253 EXPECT_EQ(0, observer->imageChangedCount()); |
| 249 EXPECT_FALSE(client->notifyFinishedCalled()); | 254 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 250 EXPECT_EQ("multipart/x-mixed-replace", imageResource->response().mimeType()); | 255 EXPECT_EQ("multipart/x-mixed-replace", imageResource->response().mimeType()); |
| 251 | 256 |
| 252 const char firstPart[] = | 257 const char firstPart[] = |
| 253 "--boundary\n" | 258 "--boundary\n" |
| 254 "Content-Type: image/svg+xml\n\n"; | 259 "Content-Type: image/svg+xml\n\n"; |
| 255 imageResource->appendData(firstPart, strlen(firstPart)); | 260 imageResource->appendData(firstPart, strlen(firstPart)); |
| 256 // Send the response for the first real part. No image or data buffer is | 261 // Send the response for the first real part. No image or data buffer is |
| 257 // created. | 262 // created. |
| 258 EXPECT_FALSE(imageResource->resourceBuffer()); | 263 EXPECT_FALSE(imageResource->resourceBuffer()); |
| 259 EXPECT_FALSE(imageResource->getContent()->hasImage()); | 264 EXPECT_FALSE(imageResource->getContent()->hasImage()); |
| 260 EXPECT_EQ(0, client->imageChangedCount()); | 265 EXPECT_EQ(0, observer->imageChangedCount()); |
| 261 EXPECT_FALSE(client->notifyFinishedCalled()); | 266 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 262 EXPECT_EQ("image/svg+xml", imageResource->response().mimeType()); | 267 EXPECT_EQ("image/svg+xml", imageResource->response().mimeType()); |
| 263 | 268 |
| 264 const char secondPart[] = | 269 const char secondPart[] = |
| 265 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect " | 270 "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect " |
| 266 "width='1' height='1' fill='green'/></svg>\n"; | 271 "width='1' height='1' fill='green'/></svg>\n"; |
| 267 // The first bytes arrive. The data buffer is created, but no image is | 272 // The first bytes arrive. The data buffer is created, but no image is |
| 268 // created. | 273 // created. |
| 269 imageResource->appendData(secondPart, strlen(secondPart)); | 274 imageResource->appendData(secondPart, strlen(secondPart)); |
| 270 EXPECT_TRUE(imageResource->resourceBuffer()); | 275 EXPECT_TRUE(imageResource->resourceBuffer()); |
| 271 EXPECT_FALSE(imageResource->getContent()->hasImage()); | 276 EXPECT_FALSE(imageResource->getContent()->hasImage()); |
| 272 EXPECT_EQ(0, client->imageChangedCount()); | 277 EXPECT_EQ(0, observer->imageChangedCount()); |
| 273 EXPECT_FALSE(client->notifyFinishedCalled()); | 278 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 274 | 279 |
| 275 // Add a client to check an assertion error doesn't happen | 280 // Add an observer to check an assertion error doesn't happen |
| 276 // (crbug.com/630983). | 281 // (crbug.com/630983). |
| 277 Persistent<MockImageResourceClient> client2 = | 282 std::unique_ptr<MockImageResourceObserver> observer2 = |
| 278 new MockImageResourceClient(imageResource); | 283 MockImageResourceObserver::create(imageResource->getContent()); |
| 279 EXPECT_EQ(0, client2->imageChangedCount()); | 284 EXPECT_EQ(0, observer2->imageChangedCount()); |
| 280 EXPECT_FALSE(client2->notifyFinishedCalled()); | 285 EXPECT_FALSE(observer2->imageNotifyFinishedCalled()); |
| 281 | 286 |
| 282 const char thirdPart[] = "--boundary"; | 287 const char thirdPart[] = "--boundary"; |
| 283 imageResource->appendData(thirdPart, strlen(thirdPart)); | 288 imageResource->appendData(thirdPart, strlen(thirdPart)); |
| 284 ASSERT_TRUE(imageResource->resourceBuffer()); | 289 ASSERT_TRUE(imageResource->resourceBuffer()); |
| 285 EXPECT_EQ(strlen(secondPart) - 1, imageResource->resourceBuffer()->size()); | 290 EXPECT_EQ(strlen(secondPart) - 1, imageResource->resourceBuffer()->size()); |
| 286 | 291 |
| 287 // This part finishes. The image is created, callbacks are sent, and the data | 292 // This part finishes. The image is created, callbacks are sent, and the data |
| 288 // buffer is cleared. | 293 // buffer is cleared. |
| 289 imageResource->loader()->didFinishLoading(0.0, 0, 0); | 294 imageResource->loader()->didFinishLoading(0.0, 0, 0); |
| 290 EXPECT_TRUE(imageResource->resourceBuffer()); | 295 EXPECT_TRUE(imageResource->resourceBuffer()); |
| 291 EXPECT_FALSE(imageResource->errorOccurred()); | 296 EXPECT_FALSE(imageResource->errorOccurred()); |
| 292 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 297 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 293 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 298 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 294 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 299 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 295 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 300 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 296 EXPECT_EQ(1, client->imageChangedCount()); | 301 EXPECT_EQ(1, observer->imageChangedCount()); |
| 297 EXPECT_TRUE(client->notifyFinishedCalled()); | 302 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 298 EXPECT_EQ(1, client2->imageChangedCount()); | 303 EXPECT_EQ(1, observer2->imageChangedCount()); |
| 299 EXPECT_TRUE(client2->notifyFinishedCalled()); | 304 EXPECT_TRUE(observer2->imageNotifyFinishedCalled()); |
| 300 } | 305 } |
| 301 | 306 |
| 302 TEST(ImageResourceTest, CancelOnDetach) { | 307 TEST(ImageResourceTest, CancelOnRemoveObserver) { |
| 303 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 308 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 304 ScopedRegisteredURL scopedRegisteredURL(testURL); | 309 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 305 | 310 |
| 306 ResourceFetcher* fetcher = | 311 ResourceFetcher* fetcher = |
| 307 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 312 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 308 | 313 |
| 309 // Emulate starting a real load. | 314 // Emulate starting a real load. |
| 310 ImageResource* imageResource = | 315 ImageResource* imageResource = |
| 311 ImageResource::create(ResourceRequest(testURL)); | 316 ImageResource::create(ResourceRequest(testURL)); |
| 312 imageResource->setIdentifier(createUniqueIdentifier()); | 317 imageResource->setIdentifier(createUniqueIdentifier()); |
| 313 | 318 |
| 314 fetcher->startLoad(imageResource); | 319 fetcher->startLoad(imageResource); |
| 315 memoryCache()->add(imageResource); | 320 memoryCache()->add(imageResource); |
| 316 | 321 |
| 317 Persistent<MockImageResourceClient> client = | 322 std::unique_ptr<MockImageResourceObserver> observer = |
| 318 new MockImageResourceClient(imageResource); | 323 MockImageResourceObserver::create(imageResource->getContent()); |
| 319 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 324 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 320 | 325 |
| 321 // The load should still be alive, but a timer should be started to cancel the | 326 // The load should still be alive, but a timer should be started to cancel the |
| 322 // load inside removeClient(). | 327 // load inside removeClient(). |
| 323 client->removeAsClient(); | 328 observer->removeAsObserver(); |
| 324 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 329 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 325 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); | 330 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); |
| 326 | 331 |
| 327 // Trigger the cancel timer, ensure the load was cancelled and the resource | 332 // Trigger the cancel timer, ensure the load was cancelled and the resource |
| 328 // was evicted from the cache. | 333 // was evicted from the cache. |
| 329 blink::testing::runPendingTasks(); | 334 blink::testing::runPendingTasks(); |
| 330 EXPECT_EQ(Resource::LoadError, imageResource->getStatus()); | 335 EXPECT_EQ(Resource::LoadError, imageResource->getStatus()); |
| 331 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); | 336 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); |
| 332 } | 337 } |
| 333 | 338 |
| 334 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { | 339 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { |
| 335 ImageResource* imageResource = ImageResource::create(ResourceRequest()); | 340 ImageResource* imageResource = ImageResource::create(ResourceRequest()); |
| 336 imageResource->setStatus(Resource::Pending); | 341 imageResource->setStatus(Resource::Pending); |
| 337 | 342 |
| 338 Persistent<MockImageResourceClient> client = | 343 std::unique_ptr<MockImageResourceObserver> observer = |
| 339 new MockImageResourceClient(imageResource); | 344 MockImageResourceObserver::create(imageResource->getContent()); |
| 340 | 345 |
| 341 // Send the image response. | 346 // Send the image response. |
| 342 imageResource->responseReceived( | 347 imageResource->responseReceived( |
| 343 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, | 348 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, |
| 344 String()), | 349 String()), |
| 345 nullptr); | 350 nullptr); |
| 346 | 351 |
| 347 imageResource->responseReceived( | 352 imageResource->responseReceived( |
| 348 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, | 353 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, |
| 349 String()), | 354 String()), |
| 350 nullptr); | 355 nullptr); |
| 351 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), | 356 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 352 sizeof(kJpegImage)); | 357 sizeof(kJpegImage)); |
| 353 EXPECT_NE(0u, imageResource->encodedSizeMemoryUsageForTesting()); | 358 EXPECT_NE(0u, imageResource->encodedSizeMemoryUsageForTesting()); |
| 354 imageResource->finish(); | 359 imageResource->finish(); |
| 355 EXPECT_EQ(0u, imageResource->encodedSizeMemoryUsageForTesting()); | 360 EXPECT_EQ(0u, imageResource->encodedSizeMemoryUsageForTesting()); |
| 356 EXPECT_FALSE(imageResource->errorOccurred()); | 361 EXPECT_FALSE(imageResource->errorOccurred()); |
| 357 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 362 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 358 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 363 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 359 EXPECT_TRUE(client->notifyFinishedCalled()); | 364 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 360 | 365 |
| 361 // The prune comes when the ImageResource still has clients. The image should | 366 // The prune comes when the ImageResource still has observers. The image |
| 362 // not be deleted. | 367 // should not be deleted. |
| 363 imageResource->prune(); | 368 imageResource->prune(); |
| 364 EXPECT_TRUE(imageResource->isAlive()); | 369 EXPECT_TRUE(imageResource->isAlive()); |
| 365 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 370 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 366 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 371 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 367 | 372 |
| 368 // The ImageResource no longer has clients. The decoded image data should be | 373 // The ImageResource no longer has observers. The decoded image data should be |
| 369 // deleted by prune. | 374 // deleted by prune. |
| 370 client->removeAsClient(); | 375 observer->removeAsObserver(); |
| 371 imageResource->prune(); | 376 imageResource->prune(); |
| 372 EXPECT_FALSE(imageResource->isAlive()); | 377 EXPECT_FALSE(imageResource->isAlive()); |
| 373 EXPECT_TRUE(imageResource->getContent()->hasImage()); | 378 EXPECT_TRUE(imageResource->getContent()->hasImage()); |
| 374 // TODO(hajimehoshi): Should check imageResource doesn't have decoded image | 379 // TODO(hajimehoshi): Should check imageResource doesn't have decoded image |
| 375 // data. | 380 // data. |
| 376 } | 381 } |
| 377 | 382 |
| 378 TEST(ImageResourceTest, UpdateBitmapImages) { | 383 TEST(ImageResourceTest, UpdateBitmapImages) { |
| 379 ImageResource* imageResource = ImageResource::create(ResourceRequest()); | 384 ImageResource* imageResource = ImageResource::create(ResourceRequest()); |
| 380 imageResource->setStatus(Resource::Pending); | 385 imageResource->setStatus(Resource::Pending); |
| 381 | 386 |
| 382 Persistent<MockImageResourceClient> client = | 387 std::unique_ptr<MockImageResourceObserver> observer = |
| 383 new MockImageResourceClient(imageResource); | 388 MockImageResourceObserver::create(imageResource->getContent()); |
| 384 | 389 |
| 385 // Send the image response. | 390 // Send the image response. |
| 386 imageResource->responseReceived( | 391 imageResource->responseReceived( |
| 387 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, | 392 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, |
| 388 String()), | 393 String()), |
| 389 nullptr); | 394 nullptr); |
| 390 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), | 395 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 391 sizeof(kJpegImage)); | 396 sizeof(kJpegImage)); |
| 392 imageResource->finish(); | 397 imageResource->finish(); |
| 393 EXPECT_FALSE(imageResource->errorOccurred()); | 398 EXPECT_FALSE(imageResource->errorOccurred()); |
| 394 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 399 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 395 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 400 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 396 EXPECT_EQ(2, client->imageChangedCount()); | 401 EXPECT_EQ(2, observer->imageChangedCount()); |
| 397 EXPECT_TRUE(client->notifyFinishedCalled()); | 402 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 398 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 403 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 399 } | 404 } |
| 400 | 405 |
| 401 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { | 406 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { |
| 402 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 407 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 403 ScopedRegisteredURL scopedRegisteredURL(testURL); | 408 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 404 ResourceRequest request = ResourceRequest(testURL); | 409 ResourceRequest request = ResourceRequest(testURL); |
| 405 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | 410 request.setPreviewsState(WebURLRequest::ServerLoFiOn); |
| 406 ImageResource* imageResource = ImageResource::create(request); | 411 ImageResource* imageResource = ImageResource::create(request); |
| 407 imageResource->setStatus(Resource::Pending); | 412 imageResource->setStatus(Resource::Pending); |
| 408 | 413 |
| 409 Persistent<MockImageResourceClient> client = | 414 std::unique_ptr<MockImageResourceObserver> observer = |
| 410 new MockImageResourceClient(imageResource); | 415 MockImageResourceObserver::create(imageResource->getContent()); |
| 411 ResourceFetcher* fetcher = | 416 ResourceFetcher* fetcher = |
| 412 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 417 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 413 | 418 |
| 414 // Send the image response. | 419 // Send the image response. |
| 415 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), | 420 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 416 nullAtom, String()); | 421 nullAtom, String()); |
| 417 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", | 422 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", |
| 418 "empty-image"); | 423 "empty-image"); |
| 419 | 424 |
| 420 imageResource->responseReceived(resourceResponse, nullptr); | 425 imageResource->responseReceived(resourceResponse, nullptr); |
| 421 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), | 426 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 422 sizeof(kJpegImage)); | 427 sizeof(kJpegImage)); |
| 423 imageResource->finish(); | 428 imageResource->finish(); |
| 424 EXPECT_FALSE(imageResource->errorOccurred()); | 429 EXPECT_FALSE(imageResource->errorOccurred()); |
| 425 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 430 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 426 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 431 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 427 EXPECT_EQ(2, client->imageChangedCount()); | 432 EXPECT_EQ(2, observer->imageChangedCount()); |
| 428 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 433 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 429 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 434 // The observer should have been notified that the image load completed. |
| 430 // The client should have been notified that the image load completed. | 435 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 431 EXPECT_TRUE(client->notifyFinishedCalled()); | 436 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 432 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | |
| 433 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | |
| 434 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 437 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 435 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 438 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 436 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 439 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 437 | 440 |
| 438 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. | 441 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. |
| 439 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, | 442 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, |
| 440 Resource::kReloadAlways); | 443 Resource::kReloadAlways); |
| 441 EXPECT_FALSE(imageResource->errorOccurred()); | 444 EXPECT_FALSE(imageResource->errorOccurred()); |
| 442 EXPECT_FALSE(imageResource->resourceBuffer()); | 445 EXPECT_FALSE(imageResource->resourceBuffer()); |
| 443 EXPECT_FALSE(imageResource->getContent()->hasImage()); | 446 EXPECT_FALSE(imageResource->getContent()->hasImage()); |
| 444 EXPECT_EQ(3, client->imageChangedCount()); | 447 EXPECT_EQ(3, observer->imageChangedCount()); |
| 445 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 448 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 446 | 449 |
| 447 imageResource->loader()->didReceiveResponse( | 450 imageResource->loader()->didReceiveResponse( |
| 448 WrappedResourceResponse(resourceResponse), nullptr); | 451 WrappedResourceResponse(resourceResponse), nullptr); |
| 449 imageResource->loader()->didReceiveData( | 452 imageResource->loader()->didReceiveData( |
| 450 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | 453 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); |
| 451 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | 454 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), |
| 452 sizeof(kJpegImage2)); | 455 sizeof(kJpegImage2)); |
| 453 EXPECT_FALSE(imageResource->errorOccurred()); | 456 EXPECT_FALSE(imageResource->errorOccurred()); |
| 454 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 457 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 455 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 458 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 456 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); | 459 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); |
| 457 EXPECT_TRUE(client->notifyFinishedCalled()); | 460 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 458 | 461 |
| 459 // The client should not have been notified of completion again. | 462 // The observer should not have been notified of completion again. |
| 460 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | 463 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 461 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | |
| 462 | 464 |
| 463 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 465 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 464 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 466 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 465 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 467 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 466 } | 468 } |
| 467 | 469 |
| 468 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { | 470 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
| 469 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 471 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 470 ScopedRegisteredURL scopedRegisteredURL(testURL); | 472 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 471 | 473 |
| 472 ResourceRequest request(testURL); | 474 ResourceRequest request(testURL); |
| 473 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | 475 request.setPreviewsState(WebURLRequest::ServerLoFiOn); |
| 474 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | 476 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
| 475 ResourceFetcher* fetcher = | 477 ResourceFetcher* fetcher = |
| 476 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 478 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 477 | 479 |
| 478 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher); | 480 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher); |
| 479 Persistent<MockImageResourceClient> client = | 481 std::unique_ptr<MockImageResourceObserver> observer = |
| 480 new MockImageResourceClient(imageResource); | 482 MockImageResourceObserver::create(imageResource->getContent()); |
| 481 | 483 |
| 482 // Send the image response. | 484 // Send the image response. |
| 483 ResourceResponse initialResourceResponse( | 485 ResourceResponse initialResourceResponse( |
| 484 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); | 486 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); |
| 485 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 487 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 486 | 488 |
| 487 imageResource->loader()->didReceiveResponse( | 489 imageResource->loader()->didReceiveResponse( |
| 488 WrappedResourceResponse(initialResourceResponse)); | 490 WrappedResourceResponse(initialResourceResponse)); |
| 489 imageResource->loader()->didReceiveData( | 491 imageResource->loader()->didReceiveData( |
| 490 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | 492 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 491 | 493 |
| 492 EXPECT_FALSE(imageResource->errorOccurred()); | 494 EXPECT_FALSE(imageResource->errorOccurred()); |
| 493 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 495 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 494 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 496 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 495 EXPECT_EQ(1, client->imageChangedCount()); | 497 EXPECT_EQ(1, observer->imageChangedCount()); |
| 496 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 498 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 497 EXPECT_FALSE(client->notifyFinishedCalled()); | 499 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 498 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 500 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 499 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 501 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 500 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 502 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 501 | 503 |
| 502 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. | 504 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. |
| 503 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, | 505 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, |
| 504 Resource::kReloadAlways); | 506 Resource::kReloadAlways); |
| 505 EXPECT_FALSE(imageResource->errorOccurred()); | 507 EXPECT_FALSE(imageResource->errorOccurred()); |
| 506 EXPECT_FALSE(imageResource->resourceBuffer()); | 508 EXPECT_FALSE(imageResource->resourceBuffer()); |
| 507 EXPECT_FALSE(imageResource->getContent()->hasImage()); | 509 EXPECT_FALSE(imageResource->getContent()->hasImage()); |
| 508 EXPECT_EQ(2, client->imageChangedCount()); | 510 EXPECT_EQ(2, observer->imageChangedCount()); |
| 509 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); | 511 EXPECT_EQ(0, observer->imageWidthOnLastImageChanged()); |
| 510 // The client should not have been notified of completion yet, since the image | 512 // The observer should not have been notified of completion yet, since the |
| 513 // image |
| 511 // is still loading. | 514 // is still loading. |
| 512 EXPECT_FALSE(client->notifyFinishedCalled()); | 515 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 513 | 516 |
| 514 imageResource->loader()->didReceiveResponse( | 517 imageResource->loader()->didReceiveResponse( |
| 515 WrappedResourceResponse(ResourceResponse( | 518 WrappedResourceResponse(ResourceResponse( |
| 516 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), | 519 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), |
| 517 nullptr); | 520 nullptr); |
| 518 imageResource->loader()->didReceiveData( | 521 imageResource->loader()->didReceiveData( |
| 519 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | 522 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); |
| 520 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | 523 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), |
| 521 sizeof(kJpegImage2)); | 524 sizeof(kJpegImage2)); |
| 522 | 525 |
| 523 EXPECT_FALSE(imageResource->errorOccurred()); | 526 EXPECT_FALSE(imageResource->errorOccurred()); |
| 524 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 527 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 525 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 528 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 526 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); | 529 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); |
| 527 // The client should have been notified of completion only after the reload | 530 // The observer should have been notified of completion only after the reload |
| 528 // completed. | 531 // completed. |
| 529 EXPECT_TRUE(client->notifyFinishedCalled()); | 532 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 530 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished()); | 533 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnImageNotifyFinished()); |
| 531 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); | |
| 532 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 534 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 533 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 535 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 534 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 536 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 535 } | 537 } |
| 536 | 538 |
| 537 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { | 539 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
| 538 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 540 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 539 ScopedRegisteredURL scopedRegisteredURL(testURL); | 541 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 540 | 542 |
| 541 ResourceFetcher* fetcher = | 543 ResourceFetcher* fetcher = |
| 542 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 544 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 543 FetchRequest request(testURL, FetchInitiatorInfo()); | 545 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 544 request.setAllowImagePlaceholder(); | 546 request.setAllowImagePlaceholder(); |
| 545 ImageResource* imageResource = ImageResource::fetch(request, fetcher); | 547 ImageResource* imageResource = ImageResource::fetch(request, fetcher); |
| 546 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 548 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 547 request.placeholderImageRequestType()); | 549 request.placeholderImageRequestType()); |
| 548 EXPECT_EQ("bytes=0-2047", | 550 EXPECT_EQ("bytes=0-2047", |
| 549 imageResource->resourceRequest().httpHeaderField("range")); | 551 imageResource->resourceRequest().httpHeaderField("range")); |
| 550 Persistent<MockImageResourceClient> client = | 552 std::unique_ptr<MockImageResourceObserver> observer = |
| 551 new MockImageResourceClient(imageResource); | 553 MockImageResourceObserver::create(imageResource->getContent()); |
| 552 | 554 |
| 553 ResourceResponse response(testURL, "image/jpeg", | 555 ResourceResponse response(testURL, "image/jpeg", |
| 554 kJpegImageSubrangeWithDimensionsLength, nullAtom, | 556 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 555 String()); | 557 String()); |
| 556 response.setHTTPStatusCode(206); | 558 response.setHTTPStatusCode(206); |
| 557 response.setHTTPHeaderField( | 559 response.setHTTPHeaderField( |
| 558 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, | 560 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 559 sizeof(kJpegImage))); | 561 sizeof(kJpegImage))); |
| 560 imageResource->loader()->didReceiveResponse( | 562 imageResource->loader()->didReceiveResponse( |
| 561 WrappedResourceResponse(response)); | 563 WrappedResourceResponse(response)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 579 EXPECT_EQ( | 581 EXPECT_EQ( |
| 580 static_cast<int>(WebCachePolicy::BypassingCache), | 582 static_cast<int>(WebCachePolicy::BypassingCache), |
| 581 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 583 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 582 | 584 |
| 583 imageResource->loader()->cancel(); | 585 imageResource->loader()->cancel(); |
| 584 } | 586 } |
| 585 | 587 |
| 586 TEST(ImageResourceTest, SVGImage) { | 588 TEST(ImageResourceTest, SVGImage) { |
| 587 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 589 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 588 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 590 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 589 Persistent<MockImageResourceClient> client = | 591 std::unique_ptr<MockImageResourceObserver> observer = |
| 590 new MockImageResourceClient(imageResource); | 592 MockImageResourceObserver::create(imageResource->getContent()); |
| 591 | 593 |
| 592 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 594 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 593 strlen(kSvgImage)); | 595 strlen(kSvgImage)); |
| 594 | 596 |
| 595 EXPECT_FALSE(imageResource->errorOccurred()); | 597 EXPECT_FALSE(imageResource->errorOccurred()); |
| 596 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 598 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 597 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 599 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 598 EXPECT_EQ(1, client->imageChangedCount()); | 600 EXPECT_EQ(1, observer->imageChangedCount()); |
| 599 EXPECT_TRUE(client->notifyFinishedCalled()); | 601 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 600 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 602 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 601 } | 603 } |
| 602 | 604 |
| 603 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { | 605 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { |
| 604 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 606 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 605 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 607 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 606 Persistent<MockImageResourceClient> client = | 608 std::unique_ptr<MockImageResourceObserver> observer = |
| 607 new MockImageResourceClient(imageResource); | 609 MockImageResourceObserver::create(imageResource->getContent()); |
| 608 | 610 |
| 609 receiveResponse(imageResource, url, "image/jpeg", | 611 receiveResponse(imageResource, url, "image/jpeg", |
| 610 reinterpret_cast<const char*>(kJpegImage), | 612 reinterpret_cast<const char*>(kJpegImage), |
| 611 sizeof(kJpegImage)); | 613 sizeof(kJpegImage)); |
| 612 | 614 |
| 613 EXPECT_FALSE(imageResource->errorOccurred()); | 615 EXPECT_FALSE(imageResource->errorOccurred()); |
| 614 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 616 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 615 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 617 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 616 EXPECT_EQ(2, client->imageChangedCount()); | 618 EXPECT_EQ(2, observer->imageChangedCount()); |
| 617 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 619 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 618 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 619 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 620 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 620 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 621 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 621 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 622 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 622 | 623 |
| 623 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 624 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 624 ResourceResponse response; | 625 ResourceResponse response; |
| 625 response.setURL(url); | 626 response.setURL(url); |
| 626 response.setHTTPStatusCode(304); | 627 response.setHTTPStatusCode(304); |
| 627 | 628 |
| 628 imageResource->responseReceived(response, nullptr); | 629 imageResource->responseReceived(response, nullptr); |
| 629 | 630 |
| 630 EXPECT_FALSE(imageResource->errorOccurred()); | 631 EXPECT_FALSE(imageResource->errorOccurred()); |
| 631 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 632 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 632 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 633 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 633 EXPECT_EQ(2, client->imageChangedCount()); | 634 EXPECT_EQ(2, observer->imageChangedCount()); |
| 634 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 635 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 635 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 636 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 636 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 637 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 637 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 638 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 638 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 639 } | 639 } |
| 640 | 640 |
| 641 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { | 641 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { |
| 642 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 642 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 643 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 643 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 644 Persistent<MockImageResourceClient> client = | 644 std::unique_ptr<MockImageResourceObserver> observer = |
| 645 new MockImageResourceClient(imageResource); | 645 MockImageResourceObserver::create(imageResource->getContent()); |
| 646 | 646 |
| 647 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 647 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 648 strlen(kSvgImage)); | 648 strlen(kSvgImage)); |
| 649 | 649 |
| 650 EXPECT_FALSE(imageResource->errorOccurred()); | 650 EXPECT_FALSE(imageResource->errorOccurred()); |
| 651 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 651 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 652 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 652 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 653 EXPECT_EQ(1, client->imageChangedCount()); | 653 EXPECT_EQ(1, observer->imageChangedCount()); |
| 654 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 654 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 655 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 656 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 655 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 657 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); | 656 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); |
| 658 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); | 657 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); |
| 659 | 658 |
| 660 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 659 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 661 ResourceResponse response; | 660 ResourceResponse response; |
| 662 response.setURL(url); | 661 response.setURL(url); |
| 663 response.setHTTPStatusCode(304); | 662 response.setHTTPStatusCode(304); |
| 664 imageResource->responseReceived(response, nullptr); | 663 imageResource->responseReceived(response, nullptr); |
| 665 | 664 |
| 666 EXPECT_FALSE(imageResource->errorOccurred()); | 665 EXPECT_FALSE(imageResource->errorOccurred()); |
| 667 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 666 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 668 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 667 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 669 EXPECT_EQ(1, client->imageChangedCount()); | 668 EXPECT_EQ(1, observer->imageChangedCount()); |
| 670 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 669 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 671 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 672 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 670 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 673 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); | 671 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); |
| 674 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); | 672 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); |
| 675 } | 673 } |
| 676 | 674 |
| 677 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { | 675 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { |
| 678 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 676 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 679 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 677 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 680 Persistent<MockImageResourceClient> client = | 678 std::unique_ptr<MockImageResourceObserver> observer = |
| 681 new MockImageResourceClient(imageResource); | 679 MockImageResourceObserver::create(imageResource->getContent()); |
| 682 | 680 |
| 683 receiveResponse(imageResource, url, "image/jpeg", | 681 receiveResponse(imageResource, url, "image/jpeg", |
| 684 reinterpret_cast<const char*>(kJpegImage), | 682 reinterpret_cast<const char*>(kJpegImage), |
| 685 sizeof(kJpegImage)); | 683 sizeof(kJpegImage)); |
| 686 | 684 |
| 687 EXPECT_FALSE(imageResource->errorOccurred()); | 685 EXPECT_FALSE(imageResource->errorOccurred()); |
| 688 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 686 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 689 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 687 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 690 EXPECT_EQ(2, client->imageChangedCount()); | 688 EXPECT_EQ(2, observer->imageChangedCount()); |
| 691 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 689 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 692 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 693 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 690 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 694 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 691 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 695 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 692 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 696 | 693 |
| 697 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 694 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 698 receiveResponse(imageResource, url, "image/jpeg", | 695 receiveResponse(imageResource, url, "image/jpeg", |
| 699 reinterpret_cast<const char*>(kJpegImage2), | 696 reinterpret_cast<const char*>(kJpegImage2), |
| 700 sizeof(kJpegImage2)); | 697 sizeof(kJpegImage2)); |
| 701 | 698 |
| 702 EXPECT_FALSE(imageResource->errorOccurred()); | 699 EXPECT_FALSE(imageResource->errorOccurred()); |
| 703 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 700 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 704 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 701 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 705 EXPECT_EQ(4, client->imageChangedCount()); | 702 EXPECT_EQ(4, observer->imageChangedCount()); |
| 706 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 703 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 707 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 708 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 704 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 709 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 705 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 710 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 706 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 711 } | 707 } |
| 712 | 708 |
| 713 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { | 709 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { |
| 714 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 710 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 715 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 711 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 716 Persistent<MockImageResourceClient> client = | 712 std::unique_ptr<MockImageResourceObserver> observer = |
| 717 new MockImageResourceClient(imageResource); | 713 MockImageResourceObserver::create(imageResource->getContent()); |
| 718 | 714 |
| 719 receiveResponse(imageResource, url, "image/jpeg", | 715 receiveResponse(imageResource, url, "image/jpeg", |
| 720 reinterpret_cast<const char*>(kJpegImage), | 716 reinterpret_cast<const char*>(kJpegImage), |
| 721 sizeof(kJpegImage)); | 717 sizeof(kJpegImage)); |
| 722 | 718 |
| 723 EXPECT_FALSE(imageResource->errorOccurred()); | 719 EXPECT_FALSE(imageResource->errorOccurred()); |
| 724 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 720 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 725 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 721 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 726 EXPECT_EQ(2, client->imageChangedCount()); | 722 EXPECT_EQ(2, observer->imageChangedCount()); |
| 727 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 723 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 728 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 729 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 724 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 730 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 725 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 731 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 726 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 732 | 727 |
| 733 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 728 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 734 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 729 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 735 strlen(kSvgImage)); | 730 strlen(kSvgImage)); |
| 736 | 731 |
| 737 EXPECT_FALSE(imageResource->errorOccurred()); | 732 EXPECT_FALSE(imageResource->errorOccurred()); |
| 738 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 733 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 739 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 734 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 740 EXPECT_EQ(3, client->imageChangedCount()); | 735 EXPECT_EQ(3, observer->imageChangedCount()); |
| 741 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 736 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 742 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 743 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 737 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 744 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); | 738 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); |
| 745 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); | 739 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); |
| 746 } | 740 } |
| 747 | 741 |
| 748 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { | 742 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { |
| 749 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 743 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 750 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 744 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 751 Persistent<MockImageResourceClient> client = | 745 std::unique_ptr<MockImageResourceObserver> observer = |
| 752 new MockImageResourceClient(imageResource); | 746 MockImageResourceObserver::create(imageResource->getContent()); |
| 753 | 747 |
| 754 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 748 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 755 strlen(kSvgImage)); | 749 strlen(kSvgImage)); |
| 756 | 750 |
| 757 EXPECT_FALSE(imageResource->errorOccurred()); | 751 EXPECT_FALSE(imageResource->errorOccurred()); |
| 758 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 752 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 759 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 753 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 760 EXPECT_EQ(1, client->imageChangedCount()); | 754 EXPECT_EQ(1, observer->imageChangedCount()); |
| 761 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 755 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 762 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 763 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 756 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 764 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); | 757 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); |
| 765 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); | 758 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); |
| 766 | 759 |
| 767 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 760 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 768 receiveResponse(imageResource, url, "image/jpeg", | 761 receiveResponse(imageResource, url, "image/jpeg", |
| 769 reinterpret_cast<const char*>(kJpegImage), | 762 reinterpret_cast<const char*>(kJpegImage), |
| 770 sizeof(kJpegImage)); | 763 sizeof(kJpegImage)); |
| 771 | 764 |
| 772 EXPECT_FALSE(imageResource->errorOccurred()); | 765 EXPECT_FALSE(imageResource->errorOccurred()); |
| 773 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 766 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 774 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 767 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 775 EXPECT_EQ(3, client->imageChangedCount()); | 768 EXPECT_EQ(3, observer->imageChangedCount()); |
| 776 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 769 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 777 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 778 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 770 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 779 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 771 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 780 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 772 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 781 } | 773 } |
| 782 | 774 |
| 783 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { | 775 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { |
| 784 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 776 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 785 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 777 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 786 Persistent<MockImageResourceClient> client = | 778 std::unique_ptr<MockImageResourceObserver> observer = |
| 787 new MockImageResourceClient(imageResource); | 779 MockImageResourceObserver::create(imageResource->getContent()); |
| 788 | 780 |
| 789 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 781 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 790 strlen(kSvgImage)); | 782 strlen(kSvgImage)); |
| 791 | 783 |
| 792 EXPECT_FALSE(imageResource->errorOccurred()); | 784 EXPECT_FALSE(imageResource->errorOccurred()); |
| 793 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 785 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 794 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 786 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 795 EXPECT_EQ(1, client->imageChangedCount()); | 787 EXPECT_EQ(1, observer->imageChangedCount()); |
| 796 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 788 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 797 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 798 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 789 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 799 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); | 790 EXPECT_EQ(200, imageResource->getContent()->getImage()->width()); |
| 800 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); | 791 EXPECT_EQ(200, imageResource->getContent()->getImage()->height()); |
| 801 | 792 |
| 802 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 793 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 803 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage2, | 794 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage2, |
| 804 strlen(kSvgImage2)); | 795 strlen(kSvgImage2)); |
| 805 | 796 |
| 806 EXPECT_FALSE(imageResource->errorOccurred()); | 797 EXPECT_FALSE(imageResource->errorOccurred()); |
| 807 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 798 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 808 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 799 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 809 EXPECT_EQ(2, client->imageChangedCount()); | 800 EXPECT_EQ(2, observer->imageChangedCount()); |
| 810 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 801 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 811 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 812 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 802 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 813 EXPECT_EQ(300, imageResource->getContent()->getImage()->width()); | 803 EXPECT_EQ(300, imageResource->getContent()->getImage()->width()); |
| 814 EXPECT_EQ(300, imageResource->getContent()->getImage()->height()); | 804 EXPECT_EQ(300, imageResource->getContent()->getImage()->height()); |
| 815 } | 805 } |
| 816 | 806 |
| 817 // Tests for pruning. | 807 // Tests for pruning. |
| 818 | 808 |
| 819 TEST(ImageResourceTest, AddClientAfterPrune) { | 809 TEST(ImageResourceTest, AddClientAfterPrune) { |
| 820 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 810 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 821 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 811 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 | 868 |
| 879 FetchRequest request(testURL, FetchInitiatorInfo()); | 869 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 880 ImageResource* imageResource = ImageResource::fetch( | 870 ImageResource* imageResource = ImageResource::fetch( |
| 881 request, | 871 request, |
| 882 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | 872 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 883 EXPECT_EQ(FetchRequest::DisallowPlaceholder, | 873 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 884 request.placeholderImageRequestType()); | 874 request.placeholderImageRequestType()); |
| 885 EXPECT_EQ(nullAtom, | 875 EXPECT_EQ(nullAtom, |
| 886 imageResource->resourceRequest().httpHeaderField("range")); | 876 imageResource->resourceRequest().httpHeaderField("range")); |
| 887 EXPECT_FALSE(imageResource->isPlaceholder()); | 877 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 888 Persistent<MockImageResourceClient> client = | 878 std::unique_ptr<MockImageResourceObserver> observer = |
| 889 new MockImageResourceClient(imageResource); | 879 MockImageResourceObserver::create(imageResource->getContent()); |
| 890 | 880 |
| 891 imageResource->loader()->didReceiveResponse( | 881 imageResource->loader()->didReceiveResponse( |
| 892 WrappedResourceResponse(ResourceResponse( | 882 WrappedResourceResponse(ResourceResponse( |
| 893 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); | 883 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 894 imageResource->loader()->didReceiveData( | 884 imageResource->loader()->didReceiveData( |
| 895 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | 885 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 896 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | 886 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 897 sizeof(kJpegImage)); | 887 sizeof(kJpegImage)); |
| 898 | 888 |
| 899 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); | 889 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); |
| 900 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | 890 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); |
| 901 EXPECT_FALSE(imageResource->isPlaceholder()); | 891 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 902 EXPECT_LT(0, client->imageChangedCount()); | 892 EXPECT_LT(0, observer->imageChangedCount()); |
| 903 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 893 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 904 EXPECT_TRUE(client->notifyFinishedCalled()); | 894 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 905 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | 895 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 906 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | |
| 907 | 896 |
| 908 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 897 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 909 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 898 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 910 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 899 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 911 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 900 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 912 } | 901 } |
| 913 | 902 |
| 914 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) { | 903 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) { |
| 915 KURL testURL(ParsedURLString, | 904 KURL testURL(ParsedURLString, |
| 916 "data:image/jpeg;base64," + | 905 "data:image/jpeg;base64," + |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 FetchRequest request(testURL, FetchInitiatorInfo()); | 962 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 974 request.setAllowImagePlaceholder(); | 963 request.setAllowImagePlaceholder(); |
| 975 ImageResource* imageResource = ImageResource::fetch( | 964 ImageResource* imageResource = ImageResource::fetch( |
| 976 request, | 965 request, |
| 977 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | 966 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 978 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 967 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 979 request.placeholderImageRequestType()); | 968 request.placeholderImageRequestType()); |
| 980 EXPECT_EQ("bytes=0-2047", | 969 EXPECT_EQ("bytes=0-2047", |
| 981 imageResource->resourceRequest().httpHeaderField("range")); | 970 imageResource->resourceRequest().httpHeaderField("range")); |
| 982 EXPECT_TRUE(imageResource->isPlaceholder()); | 971 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 983 Persistent<MockImageResourceClient> client = | 972 std::unique_ptr<MockImageResourceObserver> observer = |
| 984 new MockImageResourceClient(imageResource); | 973 MockImageResourceObserver::create(imageResource->getContent()); |
| 985 | 974 |
| 986 ResourceResponse response(testURL, "image/jpeg", | 975 ResourceResponse response(testURL, "image/jpeg", |
| 987 kJpegImageSubrangeWithDimensionsLength, nullAtom, | 976 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 988 String()); | 977 String()); |
| 989 response.setHTTPStatusCode(206); | 978 response.setHTTPStatusCode(206); |
| 990 response.setHTTPHeaderField( | 979 response.setHTTPHeaderField( |
| 991 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, | 980 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 992 sizeof(kJpegImage))); | 981 sizeof(kJpegImage))); |
| 993 imageResource->loader()->didReceiveResponse( | 982 imageResource->loader()->didReceiveResponse( |
| 994 WrappedResourceResponse(response)); | 983 WrappedResourceResponse(response)); |
| 995 imageResource->loader()->didReceiveData( | 984 imageResource->loader()->didReceiveData( |
| 996 reinterpret_cast<const char*>(kJpegImage), | 985 reinterpret_cast<const char*>(kJpegImage), |
| 997 kJpegImageSubrangeWithDimensionsLength); | 986 kJpegImageSubrangeWithDimensionsLength); |
| 998 imageResource->loader()->didFinishLoading( | 987 imageResource->loader()->didFinishLoading( |
| 999 0.0, kJpegImageSubrangeWithDimensionsLength, | 988 0.0, kJpegImageSubrangeWithDimensionsLength, |
| 1000 kJpegImageSubrangeWithDimensionsLength); | 989 kJpegImageSubrangeWithDimensionsLength); |
| 1001 | 990 |
| 1002 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); | 991 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); |
| 1003 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | 992 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 1004 imageResource->encodedSize()); | 993 imageResource->encodedSize()); |
| 1005 EXPECT_TRUE(imageResource->isPlaceholder()); | 994 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1006 EXPECT_LT(0, client->imageChangedCount()); | 995 EXPECT_LT(0, observer->imageChangedCount()); |
| 1007 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | 996 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 1008 client->encodedSizeOnLastImageChanged()); | 997 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1009 EXPECT_TRUE(client->notifyFinishedCalled()); | 998 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 1010 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | |
| 1011 client->encodedSizeOnNotifyFinished()); | |
| 1012 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | |
| 1013 client->encodedSizeOnImageNotifyFinished()); | |
| 1014 | 999 |
| 1015 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1000 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1016 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 1001 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 1017 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 1002 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 1018 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); | 1003 EXPECT_FALSE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1019 EXPECT_FALSE(imageResource->getContent()->getImage()->isSVGImage()); | 1004 EXPECT_FALSE(imageResource->getContent()->getImage()->isSVGImage()); |
| 1020 } | 1005 } |
| 1021 | 1006 |
| 1022 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { | 1007 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { |
| 1023 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1008 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1024 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1009 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1025 | 1010 |
| 1026 FetchRequest request(testURL, FetchInitiatorInfo()); | 1011 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1027 request.setAllowImagePlaceholder(); | 1012 request.setAllowImagePlaceholder(); |
| 1028 ImageResource* imageResource = ImageResource::fetch( | 1013 ImageResource* imageResource = ImageResource::fetch( |
| 1029 request, | 1014 request, |
| 1030 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | 1015 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 1031 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 1016 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1032 request.placeholderImageRequestType()); | 1017 request.placeholderImageRequestType()); |
| 1033 EXPECT_EQ("bytes=0-2047", | 1018 EXPECT_EQ("bytes=0-2047", |
| 1034 imageResource->resourceRequest().httpHeaderField("range")); | 1019 imageResource->resourceRequest().httpHeaderField("range")); |
| 1035 EXPECT_TRUE(imageResource->isPlaceholder()); | 1020 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1036 Persistent<MockImageResourceClient> client = | 1021 std::unique_ptr<MockImageResourceObserver> observer = |
| 1037 new MockImageResourceClient(imageResource); | 1022 MockImageResourceObserver::create(imageResource->getContent()); |
| 1038 | 1023 |
| 1039 const char kBadData[] = "notanimageresponse"; | 1024 const char kBadData[] = "notanimageresponse"; |
| 1040 | 1025 |
| 1041 imageResource->loader()->didReceiveResponse( | 1026 imageResource->loader()->didReceiveResponse( |
| 1042 WrappedResourceResponse(ResourceResponse( | 1027 WrappedResourceResponse(ResourceResponse( |
| 1043 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); | 1028 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); |
| 1044 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); | 1029 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); |
| 1045 | 1030 |
| 1046 // The dimensions could not be extracted, so the full original image should be | 1031 // The dimensions could not be extracted, so the full original image should be |
| 1047 // loading. | 1032 // loading. |
| 1048 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 1033 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 1049 EXPECT_FALSE(imageResource->isPlaceholder()); | 1034 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1050 EXPECT_EQ(nullAtom, | 1035 EXPECT_EQ(nullAtom, |
| 1051 imageResource->resourceRequest().httpHeaderField("range")); | 1036 imageResource->resourceRequest().httpHeaderField("range")); |
| 1052 EXPECT_EQ( | 1037 EXPECT_EQ( |
| 1053 static_cast<int>(WebCachePolicy::BypassingCache), | 1038 static_cast<int>(WebCachePolicy::BypassingCache), |
| 1054 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 1039 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1055 EXPECT_FALSE(client->notifyFinishedCalled()); | 1040 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 1056 EXPECT_EQ(0, client->imageNotifyFinishedCount()); | |
| 1057 | 1041 |
| 1058 imageResource->loader()->didReceiveResponse( | 1042 imageResource->loader()->didReceiveResponse( |
| 1059 WrappedResourceResponse(ResourceResponse( | 1043 WrappedResourceResponse(ResourceResponse( |
| 1060 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); | 1044 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 1061 imageResource->loader()->didReceiveData( | 1045 imageResource->loader()->didReceiveData( |
| 1062 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | 1046 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 1063 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | 1047 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 1064 sizeof(kJpegImage)); | 1048 sizeof(kJpegImage)); |
| 1065 | 1049 |
| 1066 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); | 1050 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); |
| 1067 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | 1051 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); |
| 1068 EXPECT_FALSE(imageResource->isPlaceholder()); | 1052 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1069 EXPECT_LT(0, client->imageChangedCount()); | 1053 EXPECT_LT(0, observer->imageChangedCount()); |
| 1070 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 1054 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 1071 EXPECT_TRUE(client->notifyFinishedCalled()); | 1055 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1072 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 1056 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 1073 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | |
| 1074 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | |
| 1075 | 1057 |
| 1076 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1058 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1077 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 1059 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 1078 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 1060 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 1079 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1061 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1080 } | 1062 } |
| 1081 | 1063 |
| 1082 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { | 1064 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
| 1083 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1065 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1084 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1066 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1085 | 1067 |
| 1086 ResourceFetcher* fetcher = | 1068 ResourceFetcher* fetcher = |
| 1087 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 1069 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1088 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1070 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1089 placeholderRequest.setAllowImagePlaceholder(); | 1071 placeholderRequest.setAllowImagePlaceholder(); |
| 1090 ImageResource* imageResource = | 1072 ImageResource* imageResource = |
| 1091 ImageResource::fetch(placeholderRequest, fetcher); | 1073 ImageResource::fetch(placeholderRequest, fetcher); |
| 1092 Persistent<MockImageResourceClient> client = | 1074 std::unique_ptr<MockImageResourceObserver> observer = |
| 1093 new MockImageResourceClient(imageResource); | 1075 MockImageResourceObserver::create(imageResource->getContent()); |
| 1094 | 1076 |
| 1095 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1077 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1096 ImageResource* secondImageResource = | 1078 ImageResource* secondImageResource = |
| 1097 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1079 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1098 EXPECT_EQ(imageResource, secondImageResource); | 1080 EXPECT_EQ(imageResource, secondImageResource); |
| 1099 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 1081 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 1100 EXPECT_FALSE(imageResource->isPlaceholder()); | 1082 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1101 EXPECT_EQ(nullAtom, | 1083 EXPECT_EQ(nullAtom, |
| 1102 imageResource->resourceRequest().httpHeaderField("range")); | 1084 imageResource->resourceRequest().httpHeaderField("range")); |
| 1103 EXPECT_EQ( | 1085 EXPECT_EQ( |
| 1104 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1086 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1105 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 1087 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1106 EXPECT_FALSE(client->notifyFinishedCalled()); | 1088 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 1107 | 1089 |
| 1108 imageResource->loader()->cancel(); | 1090 imageResource->loader()->cancel(); |
| 1109 } | 1091 } |
| 1110 | 1092 |
| 1111 TEST(ImageResourceTest, | 1093 TEST(ImageResourceTest, |
| 1112 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { | 1094 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { |
| 1113 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1095 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1114 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1096 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1115 | 1097 |
| 1116 ResourceFetcher* fetcher = | 1098 ResourceFetcher* fetcher = |
| 1117 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 1099 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1118 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1100 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1119 placeholderRequest.setAllowImagePlaceholder(); | 1101 placeholderRequest.setAllowImagePlaceholder(); |
| 1120 ImageResource* imageResource = | 1102 ImageResource* imageResource = |
| 1121 ImageResource::fetch(placeholderRequest, fetcher); | 1103 ImageResource::fetch(placeholderRequest, fetcher); |
| 1122 Persistent<MockImageResourceClient> client = | 1104 std::unique_ptr<MockImageResourceObserver> observer = |
| 1123 new MockImageResourceClient(imageResource); | 1105 MockImageResourceObserver::create(imageResource->getContent()); |
| 1124 | 1106 |
| 1125 ResourceResponse response(testURL, "image/jpeg", | 1107 ResourceResponse response(testURL, "image/jpeg", |
| 1126 kJpegImageSubrangeWithDimensionsLength, nullAtom, | 1108 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 1127 String()); | 1109 String()); |
| 1128 response.setHTTPStatusCode(206); | 1110 response.setHTTPStatusCode(206); |
| 1129 response.setHTTPHeaderField( | 1111 response.setHTTPHeaderField( |
| 1130 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, | 1112 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 1131 sizeof(kJpegImage))); | 1113 sizeof(kJpegImage))); |
| 1132 imageResource->loader()->didReceiveResponse( | 1114 imageResource->loader()->didReceiveResponse( |
| 1133 WrappedResourceResponse(response)); | 1115 WrappedResourceResponse(response)); |
| 1134 imageResource->loader()->didReceiveData( | 1116 imageResource->loader()->didReceiveData( |
| 1135 reinterpret_cast<const char*>(kJpegImage), | 1117 reinterpret_cast<const char*>(kJpegImage), |
| 1136 kJpegImageSubrangeWithDimensionsLength); | 1118 kJpegImageSubrangeWithDimensionsLength); |
| 1137 imageResource->loader()->didFinishLoading( | 1119 imageResource->loader()->didFinishLoading( |
| 1138 0.0, kJpegImageSubrangeWithDimensionsLength, | 1120 0.0, kJpegImageSubrangeWithDimensionsLength, |
| 1139 kJpegImageSubrangeWithDimensionsLength); | 1121 kJpegImageSubrangeWithDimensionsLength); |
| 1140 | 1122 |
| 1141 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); | 1123 EXPECT_EQ(Resource::Cached, imageResource->getStatus()); |
| 1142 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | 1124 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 1143 imageResource->encodedSize()); | 1125 imageResource->encodedSize()); |
| 1144 EXPECT_TRUE(imageResource->isPlaceholder()); | 1126 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1145 EXPECT_LT(0, client->imageChangedCount()); | 1127 EXPECT_LT(0, observer->imageChangedCount()); |
| 1146 EXPECT_TRUE(client->notifyFinishedCalled()); | 1128 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1147 | 1129 |
| 1148 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1130 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1149 ImageResource* secondImageResource = | 1131 ImageResource* secondImageResource = |
| 1150 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1132 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1151 EXPECT_EQ(imageResource, secondImageResource); | 1133 EXPECT_EQ(imageResource, secondImageResource); |
| 1152 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); | 1134 EXPECT_EQ(Resource::Pending, imageResource->getStatus()); |
| 1153 EXPECT_FALSE(imageResource->isPlaceholder()); | 1135 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1154 EXPECT_EQ(nullAtom, | 1136 EXPECT_EQ(nullAtom, |
| 1155 imageResource->resourceRequest().httpHeaderField("range")); | 1137 imageResource->resourceRequest().httpHeaderField("range")); |
| 1156 EXPECT_EQ( | 1138 EXPECT_EQ( |
| 1157 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1139 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1158 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 1140 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1159 | 1141 |
| 1160 imageResource->loader()->cancel(); | 1142 imageResource->loader()->cancel(); |
| 1161 } | 1143 } |
| 1162 | 1144 |
| 1163 TEST(ImageResourceTest, PeriodicFlushTest) { | 1145 TEST(ImageResourceTest, PeriodicFlushTest) { |
| 1164 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> | 1146 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 1165 platform; | 1147 platform; |
| 1166 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1148 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1167 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", | 1149 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", |
| 1168 "text/html"); | 1150 "text/html"); |
| 1169 ResourceRequest request = ResourceRequest(testURL); | 1151 ResourceRequest request = ResourceRequest(testURL); |
| 1170 ImageResource* imageResource = ImageResource::create(request); | 1152 ImageResource* imageResource = ImageResource::create(request); |
| 1171 imageResource->setStatus(Resource::Pending); | 1153 imageResource->setStatus(Resource::Pending); |
| 1172 | 1154 |
| 1173 Persistent<MockImageResourceClient> client = | 1155 std::unique_ptr<MockImageResourceObserver> observer = |
| 1174 new MockImageResourceClient(imageResource); | 1156 MockImageResourceObserver::create(imageResource->getContent()); |
| 1175 | 1157 |
| 1176 // Send the image response. | 1158 // Send the image response. |
| 1177 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), | 1159 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), |
| 1178 nullAtom, String()); | 1160 nullAtom, String()); |
| 1179 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 1161 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 1180 | 1162 |
| 1181 imageResource->responseReceived(resourceResponse, nullptr); | 1163 imageResource->responseReceived(resourceResponse, nullptr); |
| 1182 | 1164 |
| 1183 // This is number is sufficiently large amount of bytes necessary for the | 1165 // This is number is sufficiently large amount of bytes necessary for the |
| 1184 // image to be created (since the size is known). This was determined by | 1166 // image to be created (since the size is known). This was determined by |
| 1185 // appending one byte at a time (with flushes) until the image was decoded. | 1167 // appending one byte at a time (with flushes) until the image was decoded. |
| 1186 size_t meaningfulImageSize = 280; | 1168 size_t meaningfulImageSize = 280; |
| 1187 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage2), | 1169 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage2), |
| 1188 meaningfulImageSize); | 1170 meaningfulImageSize); |
| 1189 size_t bytesSent = meaningfulImageSize; | 1171 size_t bytesSent = meaningfulImageSize; |
| 1190 | 1172 |
| 1191 EXPECT_FALSE(imageResource->errorOccurred()); | 1173 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1192 EXPECT_TRUE(imageResource->getContent()->hasImage()); | 1174 EXPECT_TRUE(imageResource->getContent()->hasImage()); |
| 1193 EXPECT_EQ(1, client->imageChangedCount()); | 1175 EXPECT_EQ(1, observer->imageChangedCount()); |
| 1194 | 1176 |
| 1195 platform->runForPeriodSeconds(1.); | 1177 platform->runForPeriodSeconds(1.); |
| 1196 platform->advanceClockSeconds(1.); | 1178 platform->advanceClockSeconds(1.); |
| 1197 | 1179 |
| 1198 // Sanity check that we created an image after appending |meaningfulImageSize| | 1180 // Sanity check that we created an image after appending |meaningfulImageSize| |
| 1199 // bytes just once. | 1181 // bytes just once. |
| 1200 EXPECT_FALSE(imageResource->errorOccurred()); | 1182 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1201 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1183 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1202 EXPECT_EQ(1, client->imageChangedCount()); | 1184 EXPECT_EQ(1, observer->imageChangedCount()); |
| 1203 | 1185 |
| 1204 for (int flushCount = 1; flushCount <= 3; ++flushCount) { | 1186 for (int flushCount = 1; flushCount <= 3; ++flushCount) { |
| 1205 // For each of the iteration that appends data, we don't expect | 1187 // For each of the iteration that appends data, we don't expect |
| 1206 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 | 1188 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 |
| 1207 // seconds (it's greater than 0.2 to avoid double precision problems). | 1189 // seconds (it's greater than 0.2 to avoid double precision problems). |
| 1208 // After 5 appends, we breach the flush interval and the flush count | 1190 // After 5 appends, we breach the flush interval and the flush count |
| 1209 // increases. | 1191 // increases. |
| 1210 for (int i = 0; i < 5; ++i) { | 1192 for (int i = 0; i < 5; ++i) { |
| 1211 SCOPED_TRACE(i); | 1193 SCOPED_TRACE(i); |
| 1212 imageResource->appendData( | 1194 imageResource->appendData( |
| 1213 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); | 1195 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); |
| 1214 | 1196 |
| 1215 EXPECT_FALSE(imageResource->errorOccurred()); | 1197 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1216 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1198 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1217 EXPECT_EQ(flushCount, client->imageChangedCount()); | 1199 EXPECT_EQ(flushCount, observer->imageChangedCount()); |
| 1218 | 1200 |
| 1219 ++bytesSent; | 1201 ++bytesSent; |
| 1220 platform->runForPeriodSeconds(0.2001); | 1202 platform->runForPeriodSeconds(0.2001); |
| 1221 } | 1203 } |
| 1222 } | 1204 } |
| 1223 | 1205 |
| 1224 // Increasing time by a large number only causes one extra flush. | 1206 // Increasing time by a large number only causes one extra flush. |
| 1225 platform->runForPeriodSeconds(10.); | 1207 platform->runForPeriodSeconds(10.); |
| 1226 platform->advanceClockSeconds(10.); | 1208 platform->advanceClockSeconds(10.); |
| 1227 EXPECT_FALSE(imageResource->errorOccurred()); | 1209 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1228 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1210 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1229 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 1211 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 1230 EXPECT_EQ(4, client->imageChangedCount()); | 1212 EXPECT_EQ(4, observer->imageChangedCount()); |
| 1231 | 1213 |
| 1232 // Append the rest of the data and finish (which causes another flush). | 1214 // Append the rest of the data and finish (which causes another flush). |
| 1233 imageResource->appendData( | 1215 imageResource->appendData( |
| 1234 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, | 1216 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, |
| 1235 sizeof(kJpegImage2) - bytesSent); | 1217 sizeof(kJpegImage2) - bytesSent); |
| 1236 imageResource->finish(); | 1218 imageResource->finish(); |
| 1237 | 1219 |
| 1238 EXPECT_FALSE(imageResource->errorOccurred()); | 1220 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1239 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1221 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1240 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 1222 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 1241 EXPECT_EQ(5, client->imageChangedCount()); | 1223 EXPECT_EQ(5, observer->imageChangedCount()); |
| 1242 EXPECT_TRUE(client->notifyFinishedCalled()); | 1224 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1243 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1225 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1244 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 1226 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 1245 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 1227 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 1246 | 1228 |
| 1247 WTF::setTimeFunctionsForTesting(nullptr); | 1229 WTF::setTimeFunctionsForTesting(nullptr); |
| 1248 } | 1230 } |
| 1249 | 1231 |
| 1250 } // namespace | 1232 } // namespace |
| 1251 } // namespace blink | 1233 } // namespace blink |
| OLD | NEW |