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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 135 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
136 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, | 136 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, |
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
138 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, | 138 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, |
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
140 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, | 140 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, |
141 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, | 141 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, |
142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
143 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; | 143 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; |
144 | 144 |
145 constexpr int kJpegImage2Width = 50; | |
146 | |
147 constexpr char kSvgImage[] = | 145 constexpr char kSvgImage[] = |
148 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " | 146 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " |
149 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | 147 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
150 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" | 148 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" |
151 "</svg>"; | 149 "</svg>"; |
152 | 150 |
153 constexpr char kSvgImage2[] = | 151 constexpr char kSvgImage2[] = |
154 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " | 152 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " |
155 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | 153 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
156 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" | 154 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" |
(...skipping 12 matching lines...) Expand all Loading... |
169 size_t dataSize) { | 167 size_t dataSize) { |
170 ResourceResponse response; | 168 ResourceResponse response; |
171 response.setURL(url); | 169 response.setURL(url); |
172 response.setHTTPStatusCode(200); | 170 response.setHTTPStatusCode(200); |
173 response.setMimeType(mimeType); | 171 response.setMimeType(mimeType); |
174 imageResource->responseReceived(response, nullptr); | 172 imageResource->responseReceived(response, nullptr); |
175 imageResource->appendData(data, dataSize); | 173 imageResource->appendData(data, dataSize); |
176 imageResource->finish(); | 174 imageResource->finish(); |
177 } | 175 } |
178 | 176 |
| 177 void testThatReloadIsStartedThenServeReload(const KURL& testURL, |
| 178 ImageResource* imageResource, |
| 179 ImageResourceContent* content, |
| 180 MockImageResourceObserver* observer, |
| 181 WebCachePolicy policyForReload) { |
| 182 const char* data = reinterpret_cast<const char*>(kJpegImage2); |
| 183 constexpr size_t dataLength = sizeof(kJpegImage2); |
| 184 constexpr int imageWidth = 50; |
| 185 constexpr int imageHeight = 50; |
| 186 |
| 187 // Checks that |imageResource| and |content| are ready for non-placeholder |
| 188 // reloading. |
| 189 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); |
| 190 EXPECT_FALSE(imageResource->resourceBuffer()); |
| 191 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 192 EXPECT_EQ(nullAtom, |
| 193 imageResource->resourceRequest().httpHeaderField("range")); |
| 194 EXPECT_EQ(policyForReload, imageResource->resourceRequest().getCachePolicy()); |
| 195 EXPECT_EQ(content, imageResource->getContent()); |
| 196 EXPECT_FALSE(content->hasImage()); |
| 197 |
| 198 // Checks |observer| before reloading. |
| 199 const int originalImageChangedCount = observer->imageChangedCount(); |
| 200 const bool alreadyNotifiedFinish = observer->imageNotifyFinishedCalled(); |
| 201 const int imageWidthOnImageNotifyFinished = |
| 202 observer->imageWidthOnImageNotifyFinished(); |
| 203 ASSERT_NE(imageWidth, imageWidthOnImageNotifyFinished); |
| 204 |
| 205 // Does Reload. |
| 206 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( |
| 207 ResourceResponse(testURL, "image/jpeg", dataLength, nullAtom))); |
| 208 imageResource->loader()->didReceiveData(data, dataLength); |
| 209 imageResource->loader()->didFinishLoading(0.0, dataLength, dataLength); |
| 210 |
| 211 // Checks |imageResource|'s status after reloading. |
| 212 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); |
| 213 EXPECT_FALSE(imageResource->errorOccurred()); |
| 214 EXPECT_EQ(dataLength, imageResource->encodedSize()); |
| 215 |
| 216 // Checks |observer| after reloading that it is notified of updates/finish. |
| 217 EXPECT_LT(originalImageChangedCount, observer->imageChangedCount()); |
| 218 EXPECT_EQ(imageWidth, observer->imageWidthOnLastImageChanged()); |
| 219 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 220 if (!alreadyNotifiedFinish) { |
| 221 // If imageNotifyFinished() has not been called before the reloaded |
| 222 // response is served, then imageNotifyFinished() should be called with |
| 223 // the new image (of width |imageWidth|). |
| 224 EXPECT_EQ(imageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 225 } |
| 226 |
| 227 // Checks |content| receives the correct image. |
| 228 EXPECT_TRUE(content->hasImage()); |
| 229 EXPECT_FALSE(content->getImage()->isNull()); |
| 230 EXPECT_EQ(imageWidth, content->getImage()->width()); |
| 231 EXPECT_EQ(imageHeight, content->getImage()->height()); |
| 232 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 233 } |
| 234 |
179 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { | 235 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { |
180 return AtomicString(String("bytes 0-" + String::number(rangeLength - 1) + | 236 return AtomicString(String("bytes 0-" + String::number(rangeLength - 1) + |
181 "/" + String::number(totalLength))); | 237 "/" + String::number(totalLength))); |
182 } | 238 } |
183 | 239 |
184 ResourceFetcher* createFetcher() { | 240 ResourceFetcher* createFetcher() { |
185 return ResourceFetcher::create( | 241 return ResourceFetcher::create( |
186 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); | 242 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); |
187 } | 243 } |
188 | 244 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 // The observer should have been notified that the image load completed. | 447 // The observer should have been notified that the image load completed. |
392 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 448 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
393 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | 449 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
394 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 450 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
395 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 451 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
396 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 452 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
397 | 453 |
398 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. | 454 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. |
399 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, | 455 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, |
400 Resource::kReloadAlways); | 456 Resource::kReloadAlways); |
401 EXPECT_FALSE(imageResource->errorOccurred()); | 457 |
402 EXPECT_FALSE(imageResource->resourceBuffer()); | |
403 EXPECT_FALSE(imageResource->getContent()->hasImage()); | |
404 EXPECT_EQ(3, observer->imageChangedCount()); | 458 EXPECT_EQ(3, observer->imageChangedCount()); |
405 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 459 testThatReloadIsStartedThenServeReload( |
406 | 460 testURL, imageResource, imageResource->getContent(), observer.get(), |
407 imageResource->loader()->didReceiveResponse( | 461 WebCachePolicy::BypassingCache); |
408 WrappedResourceResponse(resourceResponse), nullptr); | |
409 imageResource->loader()->didReceiveData( | |
410 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | |
411 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | |
412 sizeof(kJpegImage2)); | |
413 EXPECT_FALSE(imageResource->errorOccurred()); | |
414 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
415 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | |
416 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); | |
417 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
418 | |
419 // The observer should not have been notified of completion again. | |
420 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
421 | |
422 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
423 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | |
424 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | |
425 } | 462 } |
426 | 463 |
427 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) { | 464 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) { |
428 ResourceFetcher* fetcher = createFetcher(); | 465 ResourceFetcher* fetcher = createFetcher(); |
429 | 466 |
430 KURL testURL(ParsedURLString, kTestURL); | 467 KURL testURL(ParsedURLString, kTestURL); |
431 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 468 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
432 | 469 |
433 ResourceRequest request = ResourceRequest(testURL); | 470 ResourceRequest request = ResourceRequest(testURL); |
434 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | 471 request.setPreviewsState(WebURLRequest::ServerLoFiOn); |
(...skipping 10 matching lines...) Expand all Loading... |
445 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", | 482 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", |
446 "empty-image"); | 483 "empty-image"); |
447 | 484 |
448 imageResource->loader()->didReceiveResponse( | 485 imageResource->loader()->didReceiveResponse( |
449 WrappedResourceResponse(resourceResponse)); | 486 WrappedResourceResponse(resourceResponse)); |
450 imageResource->loader()->didReceiveData( | 487 imageResource->loader()->didReceiveData( |
451 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | 488 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
452 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | 489 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
453 sizeof(kJpegImage)); | 490 sizeof(kJpegImage)); |
454 | 491 |
| 492 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
455 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL)); | 493 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL)); |
456 | 494 |
457 fetcher->reloadLoFiImages(); | 495 fetcher->reloadLoFiImages(); |
458 | 496 |
459 EXPECT_FALSE(imageResource->errorOccurred()); | |
460 EXPECT_FALSE(imageResource->resourceBuffer()); | |
461 EXPECT_FALSE(imageResource->getContent()->hasImage()); | |
462 EXPECT_EQ(3, observer->imageChangedCount()); | 497 EXPECT_EQ(3, observer->imageChangedCount()); |
463 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
464 | 498 |
465 imageResource->loader()->didReceiveResponse( | 499 testThatReloadIsStartedThenServeReload(testURL, imageResource, content, |
466 WrappedResourceResponse(resourceResponse), nullptr); | 500 observer.get(), |
467 imageResource->loader()->didReceiveData( | 501 WebCachePolicy::BypassingCache); |
468 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | |
469 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | |
470 sizeof(kJpegImage2)); | |
471 EXPECT_FALSE(imageResource->errorOccurred()); | |
472 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
473 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | |
474 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); | |
475 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
476 | |
477 // The observer should not have been notified of completion again. | |
478 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
479 | |
480 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
481 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | |
482 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | |
483 | 502 |
484 memoryCache()->remove(imageResource); | 503 memoryCache()->remove(imageResource); |
485 } | 504 } |
486 | 505 |
487 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { | 506 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
488 KURL testURL(ParsedURLString, kTestURL); | 507 KURL testURL(ParsedURLString, kTestURL); |
489 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 508 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
490 | 509 |
491 ResourceRequest request(testURL); | 510 ResourceRequest request(testURL); |
492 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | 511 request.setPreviewsState(WebURLRequest::ServerLoFiOn); |
(...skipping 20 matching lines...) Expand all Loading... |
513 EXPECT_EQ(1, observer->imageChangedCount()); | 532 EXPECT_EQ(1, observer->imageChangedCount()); |
514 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | 533 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
515 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | 534 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
516 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 535 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
517 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 536 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
518 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 537 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
519 | 538 |
520 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. | 539 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. |
521 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, | 540 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, |
522 Resource::kReloadAlways); | 541 Resource::kReloadAlways); |
523 EXPECT_FALSE(imageResource->errorOccurred()); | 542 |
524 EXPECT_FALSE(imageResource->resourceBuffer()); | |
525 EXPECT_FALSE(imageResource->getContent()->hasImage()); | |
526 EXPECT_EQ(2, observer->imageChangedCount()); | 543 EXPECT_EQ(2, observer->imageChangedCount()); |
527 EXPECT_EQ(0, observer->imageWidthOnLastImageChanged()); | 544 EXPECT_EQ(0, observer->imageWidthOnLastImageChanged()); |
528 // The observer should not have been notified of completion yet, since the | 545 // The observer should not have been notified of completion yet, since the |
529 // image | 546 // image is still loading. |
530 // is still loading. | |
531 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | 547 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
532 | 548 |
533 imageResource->loader()->didReceiveResponse( | 549 testThatReloadIsStartedThenServeReload( |
534 WrappedResourceResponse(ResourceResponse(testURL, "image/jpeg", | 550 testURL, imageResource, imageResource->getContent(), observer.get(), |
535 sizeof(kJpegImage2), nullAtom)), | 551 WebCachePolicy::BypassingCache); |
536 nullptr); | |
537 imageResource->loader()->didReceiveData( | |
538 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | |
539 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | |
540 sizeof(kJpegImage2)); | |
541 | |
542 EXPECT_FALSE(imageResource->errorOccurred()); | |
543 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
544 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | |
545 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); | |
546 // The observer should have been notified of completion only after the reload | |
547 // completed. | |
548 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
549 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnImageNotifyFinished()); | |
550 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
551 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | |
552 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | |
553 } | 552 } |
554 | 553 |
555 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { | 554 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
556 KURL testURL(ParsedURLString, kTestURL); | 555 KURL testURL(ParsedURLString, kTestURL); |
557 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 556 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
558 | 557 |
559 ResourceFetcher* fetcher = createFetcher(); | 558 ResourceFetcher* fetcher = createFetcher(); |
560 FetchRequest request(testURL, FetchInitiatorInfo()); | 559 FetchRequest request(testURL, FetchInitiatorInfo()); |
561 request.setAllowImagePlaceholder(); | 560 request.setAllowImagePlaceholder(); |
562 ImageResource* imageResource = ImageResource::fetch(request, fetcher); | 561 ImageResource* imageResource = ImageResource::fetch(request, fetcher); |
(...skipping 14 matching lines...) Expand all Loading... |
577 WrappedResourceResponse(response)); | 576 WrappedResourceResponse(response)); |
578 imageResource->loader()->didReceiveData( | 577 imageResource->loader()->didReceiveData( |
579 reinterpret_cast<const char*>(kJpegImage), | 578 reinterpret_cast<const char*>(kJpegImage), |
580 kJpegImageSubrangeWithDimensionsLength); | 579 kJpegImageSubrangeWithDimensionsLength); |
581 imageResource->loader()->didFinishLoading( | 580 imageResource->loader()->didFinishLoading( |
582 0.0, kJpegImageSubrangeWithDimensionsLength, | 581 0.0, kJpegImageSubrangeWithDimensionsLength, |
583 kJpegImageSubrangeWithDimensionsLength); | 582 kJpegImageSubrangeWithDimensionsLength); |
584 | 583 |
585 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); | 584 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); |
586 EXPECT_TRUE(imageResource->isPlaceholder()); | 585 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 586 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
587 | 587 |
588 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, | 588 imageResource->reloadIfLoFiOrPlaceholderImage(fetcher, |
589 Resource::kReloadAlways); | 589 Resource::kReloadAlways); |
590 | 590 |
591 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | 591 testThatReloadIsStartedThenServeReload( |
592 EXPECT_FALSE(imageResource->isPlaceholder()); | 592 testURL, imageResource, imageResource->getContent(), observer.get(), |
593 EXPECT_EQ(nullAtom, | 593 WebCachePolicy::BypassingCache); |
594 imageResource->resourceRequest().httpHeaderField("range")); | |
595 EXPECT_EQ( | |
596 static_cast<int>(WebCachePolicy::BypassingCache), | |
597 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
598 | |
599 imageResource->loader()->cancel(); | |
600 } | 594 } |
601 | 595 |
602 TEST(ImageResourceTest, SVGImage) { | 596 TEST(ImageResourceTest, SVGImage) { |
603 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 597 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
604 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 598 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
605 std::unique_ptr<MockImageResourceObserver> observer = | 599 std::unique_ptr<MockImageResourceObserver> observer = |
606 MockImageResourceObserver::create(imageResource->getContent()); | 600 MockImageResourceObserver::create(imageResource->getContent()); |
607 | 601 |
608 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 602 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
609 strlen(kSvgImage)); | 603 strlen(kSvgImage)); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1114 | 1108 |
1115 imageResource->loader()->didReceiveResponse( | 1109 imageResource->loader()->didReceiveResponse( |
1116 WrappedResourceResponse(badResponse)); | 1110 WrappedResourceResponse(badResponse)); |
1117 | 1111 |
1118 EXPECT_EQ(0, observer->imageChangedCount()); | 1112 EXPECT_EQ(0, observer->imageChangedCount()); |
1119 | 1113 |
1120 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); | 1114 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); |
1121 | 1115 |
1122 // The dimensions could not be extracted, so the full original image should be | 1116 // The dimensions could not be extracted, so the full original image should be |
1123 // loading. | 1117 // loading. |
1124 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
1125 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1126 EXPECT_EQ(nullAtom, | |
1127 imageResource->resourceRequest().httpHeaderField("range")); | |
1128 EXPECT_EQ( | |
1129 static_cast<int>(WebCachePolicy::BypassingCache), | |
1130 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1131 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | 1118 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
1132 EXPECT_EQ(3, observer->imageChangedCount()); | 1119 EXPECT_EQ(3, observer->imageChangedCount()); |
1133 | 1120 |
1134 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( | 1121 testThatReloadIsStartedThenServeReload( |
1135 ResourceResponse(testURL, "image/jpeg", sizeof(kJpegImage), nullAtom))); | 1122 testURL, imageResource, imageResource->getContent(), observer.get(), |
1136 imageResource->loader()->didReceiveData( | 1123 WebCachePolicy::BypassingCache); |
1137 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
1138 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | |
1139 sizeof(kJpegImage)); | |
1140 | |
1141 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); | |
1142 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | |
1143 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1144 EXPECT_LT(3, observer->imageChangedCount()); | |
1145 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | |
1146 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
1147 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
1148 | |
1149 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
1150 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | |
1151 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | |
1152 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
1153 } | 1124 } |
1154 | 1125 |
1155 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) { | 1126 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) { |
1156 KURL testURL(ParsedURLString, kTestURL); | 1127 KURL testURL(ParsedURLString, kTestURL); |
1157 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 1128 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
1158 | 1129 |
1159 FetchRequest request(testURL, FetchInitiatorInfo()); | 1130 FetchRequest request(testURL, FetchInitiatorInfo()); |
1160 request.setAllowImagePlaceholder(); | 1131 request.setAllowImagePlaceholder(); |
1161 ImageResource* imageResource = ImageResource::fetch(request, createFetcher()); | 1132 ImageResource* imageResource = ImageResource::fetch(request, createFetcher()); |
1162 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 1133 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
(...skipping 20 matching lines...) Expand all Loading... |
1183 imageResource->loader()->didReceiveData( | 1154 imageResource->loader()->didReceiveData( |
1184 reinterpret_cast<const char*>(kJpegImage), | 1155 reinterpret_cast<const char*>(kJpegImage), |
1185 kJpegImageSubrangeWithoutDimensionsLength); | 1156 kJpegImageSubrangeWithoutDimensionsLength); |
1186 | 1157 |
1187 EXPECT_EQ(0, observer->imageChangedCount()); | 1158 EXPECT_EQ(0, observer->imageChangedCount()); |
1188 | 1159 |
1189 imageResource->loader()->didFinishLoading( | 1160 imageResource->loader()->didFinishLoading( |
1190 0.0, kJpegImageSubrangeWithoutDimensionsLength, | 1161 0.0, kJpegImageSubrangeWithoutDimensionsLength, |
1191 kJpegImageSubrangeWithoutDimensionsLength); | 1162 kJpegImageSubrangeWithoutDimensionsLength); |
1192 | 1163 |
1193 // Decode error didn't occur but the dimensions could not be extracted, | |
1194 // so the full original image should be loading. | |
1195 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
1196 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1197 EXPECT_EQ(nullAtom, | |
1198 imageResource->resourceRequest().httpHeaderField("range")); | |
1199 EXPECT_EQ( | |
1200 static_cast<int>(WebCachePolicy::BypassingCache), | |
1201 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1202 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | 1164 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
1203 EXPECT_EQ(2, observer->imageChangedCount()); | 1165 EXPECT_EQ(2, observer->imageChangedCount()); |
1204 | 1166 |
1205 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( | 1167 testThatReloadIsStartedThenServeReload( |
1206 ResourceResponse(testURL, "image/jpeg", sizeof(kJpegImage), nullAtom))); | 1168 testURL, imageResource, imageResource->getContent(), observer.get(), |
1207 imageResource->loader()->didReceiveData( | 1169 WebCachePolicy::BypassingCache); |
1208 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
1209 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | |
1210 sizeof(kJpegImage)); | |
1211 | |
1212 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); | |
1213 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | |
1214 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1215 EXPECT_LT(0, observer->imageChangedCount()); | |
1216 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | |
1217 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
1218 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
1219 | |
1220 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
1221 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | |
1222 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | |
1223 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
1224 } | 1170 } |
1225 | 1171 |
1226 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { | 1172 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
1227 KURL testURL(ParsedURLString, kTestURL); | 1173 KURL testURL(ParsedURLString, kTestURL); |
1228 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 1174 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
1229 | 1175 |
1230 ResourceFetcher* fetcher = createFetcher(); | 1176 ResourceFetcher* fetcher = createFetcher(); |
1231 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1177 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
1232 placeholderRequest.setAllowImagePlaceholder(); | 1178 placeholderRequest.setAllowImagePlaceholder(); |
1233 ImageResource* imageResource = | 1179 ImageResource* imageResource = |
1234 ImageResource::fetch(placeholderRequest, fetcher); | 1180 ImageResource::fetch(placeholderRequest, fetcher); |
1235 std::unique_ptr<MockImageResourceObserver> observer = | 1181 std::unique_ptr<MockImageResourceObserver> observer = |
1236 MockImageResourceObserver::create(imageResource->getContent()); | 1182 MockImageResourceObserver::create(imageResource->getContent()); |
1237 | 1183 |
1238 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1184 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
1239 ImageResource* secondImageResource = | 1185 ImageResource* secondImageResource = |
1240 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1186 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1187 |
1241 EXPECT_EQ(imageResource, secondImageResource); | 1188 EXPECT_EQ(imageResource, secondImageResource); |
1242 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
1243 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1244 EXPECT_EQ(nullAtom, | |
1245 imageResource->resourceRequest().httpHeaderField("range")); | |
1246 EXPECT_EQ( | |
1247 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | |
1248 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1249 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | 1189 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
1250 | 1190 |
1251 imageResource->loader()->cancel(); | 1191 testThatReloadIsStartedThenServeReload( |
| 1192 testURL, imageResource, imageResource->getContent(), observer.get(), |
| 1193 WebCachePolicy::UseProtocolCachePolicy); |
1252 } | 1194 } |
1253 | 1195 |
1254 TEST(ImageResourceTest, | 1196 TEST(ImageResourceTest, |
1255 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { | 1197 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { |
1256 KURL testURL(ParsedURLString, kTestURL); | 1198 KURL testURL(ParsedURLString, kTestURL); |
1257 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 1199 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
1258 | 1200 |
1259 ResourceFetcher* fetcher = createFetcher(); | 1201 ResourceFetcher* fetcher = createFetcher(); |
1260 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1202 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
1261 placeholderRequest.setAllowImagePlaceholder(); | 1203 placeholderRequest.setAllowImagePlaceholder(); |
(...skipping 21 matching lines...) Expand all Loading... |
1283 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, | 1225 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
1284 imageResource->encodedSize()); | 1226 imageResource->encodedSize()); |
1285 EXPECT_TRUE(imageResource->isPlaceholder()); | 1227 EXPECT_TRUE(imageResource->isPlaceholder()); |
1286 EXPECT_LT(0, observer->imageChangedCount()); | 1228 EXPECT_LT(0, observer->imageChangedCount()); |
1287 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 1229 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
1288 | 1230 |
1289 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1231 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
1290 ImageResource* secondImageResource = | 1232 ImageResource* secondImageResource = |
1291 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1233 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
1292 EXPECT_EQ(imageResource, secondImageResource); | 1234 EXPECT_EQ(imageResource, secondImageResource); |
1293 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
1294 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1295 EXPECT_EQ(nullAtom, | |
1296 imageResource->resourceRequest().httpHeaderField("range")); | |
1297 EXPECT_EQ( | |
1298 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | |
1299 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1300 | 1235 |
1301 imageResource->loader()->cancel(); | 1236 testThatReloadIsStartedThenServeReload( |
| 1237 testURL, imageResource, imageResource->getContent(), observer.get(), |
| 1238 WebCachePolicy::UseProtocolCachePolicy); |
1302 } | 1239 } |
1303 | 1240 |
1304 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) { | 1241 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) { |
1305 const struct { | 1242 const struct { |
1306 int statusCode; | 1243 int statusCode; |
1307 AtomicString contentRange; | 1244 AtomicString contentRange; |
1308 } tests[] = { | 1245 } tests[] = { |
1309 {200, nullAtom}, | 1246 {200, nullAtom}, |
1310 {404, nullAtom}, | 1247 {404, nullAtom}, |
1311 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))}, | 1248 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))}, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 static const char kBadImageData[] = "bad image data"; | 1355 static const char kBadImageData[] = "bad image data"; |
1419 | 1356 |
1420 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData), | 1357 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData), |
1421 nullAtom); | 1358 nullAtom); |
1422 response.setHTTPStatusCode(statusCode); | 1359 response.setHTTPStatusCode(statusCode); |
1423 imageResource->loader()->didReceiveResponse( | 1360 imageResource->loader()->didReceiveResponse( |
1424 WrappedResourceResponse(response)); | 1361 WrappedResourceResponse(response)); |
1425 imageResource->loader()->didReceiveData(kBadImageData, | 1362 imageResource->loader()->didReceiveData(kBadImageData, |
1426 sizeof(kBadImageData)); | 1363 sizeof(kBadImageData)); |
1427 | 1364 |
| 1365 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 1366 |
1428 // The dimensions could not be extracted, and the response code was a 4xx | 1367 // The dimensions could not be extracted, and the response code was a 4xx |
1429 // error, so the full original image should be loading. | 1368 // error, so the full original image should be loading. |
1430 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | 1369 testThatReloadIsStartedThenServeReload( |
1431 EXPECT_FALSE(imageResource->isPlaceholder()); | 1370 testURL, imageResource, imageResource->getContent(), observer.get(), |
1432 EXPECT_EQ(nullAtom, | 1371 WebCachePolicy::BypassingCache); |
1433 imageResource->resourceRequest().httpHeaderField("range")); | |
1434 EXPECT_EQ( | |
1435 static_cast<int>(WebCachePolicy::BypassingCache), | |
1436 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1437 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | |
1438 | |
1439 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( | |
1440 ResourceResponse(testURL, "image/jpeg", sizeof(kJpegImage), nullAtom))); | |
1441 imageResource->loader()->didReceiveData( | |
1442 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
1443 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | |
1444 sizeof(kJpegImage)); | |
1445 | |
1446 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); | |
1447 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | |
1448 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1449 EXPECT_LT(0, observer->imageChangedCount()); | |
1450 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | |
1451 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
1452 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
1453 | |
1454 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
1455 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | |
1456 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | |
1457 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
1458 } | 1372 } |
1459 } | 1373 } |
1460 | 1374 |
1461 TEST(ImageResourceTest, PeriodicFlushTest) { | 1375 TEST(ImageResourceTest, PeriodicFlushTest) { |
1462 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> | 1376 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
1463 platform; | 1377 platform; |
1464 KURL testURL(ParsedURLString, kTestURL); | 1378 KURL testURL(ParsedURLString, kTestURL); |
1465 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 1379 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
1466 ResourceRequest request = ResourceRequest(testURL); | 1380 ResourceRequest request = ResourceRequest(testURL); |
1467 ImageResource* imageResource = ImageResource::create(request); | 1381 ImageResource* imageResource = ImageResource::create(request); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1539 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 1453 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
1540 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1454 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
1541 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 1455 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
1542 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 1456 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
1543 | 1457 |
1544 WTF::setTimeFunctionsForTesting(nullptr); | 1458 WTF::setTimeFunctionsForTesting(nullptr); |
1545 } | 1459 } |
1546 | 1460 |
1547 } // namespace | 1461 } // namespace |
1548 } // namespace blink | 1462 } // namespace blink |
OLD | NEW |