| 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 12 matching lines...) Expand all Loading... |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/fetch/ImageResource.h" | 31 #include "core/fetch/ImageResource.h" |
| 32 | 32 |
| 33 #include "core/fetch/ImageResourceObserver.h" |
| 33 #include "core/fetch/MemoryCache.h" | 34 #include "core/fetch/MemoryCache.h" |
| 34 #include "core/fetch/MockResourceClients.h" | 35 #include "core/fetch/MockResourceClients.h" |
| 35 #include "core/fetch/ResourceFetcher.h" | 36 #include "core/fetch/ResourceFetcher.h" |
| 36 #include "core/fetch/ResourceLoader.h" | 37 #include "core/fetch/ResourceLoader.h" |
| 37 #include "core/fetch/UniqueIdentifier.h" | 38 #include "core/fetch/UniqueIdentifier.h" |
| 38 #include "platform/SharedBuffer.h" | 39 #include "platform/SharedBuffer.h" |
| 39 #include "platform/exported/WrappedResourceResponse.h" | 40 #include "platform/exported/WrappedResourceResponse.h" |
| 41 #include "platform/geometry/IntSize.h" |
| 42 #include "platform/graphics/BitmapImage.h" |
| 40 #include "platform/graphics/Image.h" | 43 #include "platform/graphics/Image.h" |
| 44 #include "platform/network/ResourceError.h" |
| 41 #include "platform/scheduler/test/fake_web_task_runner.h" | 45 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 42 #include "platform/testing/URLTestHelpers.h" | 46 #include "platform/testing/URLTestHelpers.h" |
| 43 #include "platform/testing/UnitTestHelpers.h" | 47 #include "platform/testing/UnitTestHelpers.h" |
| 44 #include "public/platform/Platform.h" | 48 #include "public/platform/Platform.h" |
| 49 #include "public/platform/WebCachePolicy.h" |
| 45 #include "public/platform/WebURL.h" | 50 #include "public/platform/WebURL.h" |
| 46 #include "public/platform/WebURLLoaderMockFactory.h" | 51 #include "public/platform/WebURLLoaderMockFactory.h" |
| 47 #include "public/platform/WebURLResponse.h" | 52 #include "public/platform/WebURLResponse.h" |
| 48 #include "testing/gtest/include/gtest/gtest.h" | 53 #include "testing/gtest/include/gtest/gtest.h" |
| 49 #include "wtf/PtrUtil.h" | 54 #include "wtf/PtrUtil.h" |
| 55 #include "wtf/text/Base64.h" |
| 50 #include <memory> | 56 #include <memory> |
| 51 | 57 |
| 52 namespace blink { | 58 namespace blink { |
| 53 | 59 |
| 54 namespace { | 60 namespace { |
| 55 | 61 |
| 56 // An image of size 1x1. | 62 // An image of size 1x1. |
| 57 static Vector<unsigned char> jpegImage() | 63 static Vector<unsigned char> jpegImage() |
| 58 { | 64 { |
| 59 Vector<unsigned char> jpeg; | 65 Vector<unsigned char> jpeg; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 80 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | 86 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, |
| 81 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
0x11, 0x00, 0x3f, | 87 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
0x11, 0x00, 0x3f, |
| 82 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9 | 88 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9 |
| 83 }; | 89 }; |
| 84 | 90 |
| 85 jpeg.append(data, sizeof(data)); | 91 jpeg.append(data, sizeof(data)); |
| 86 return jpeg; | 92 return jpeg; |
| 87 } | 93 } |
| 88 | 94 |
| 89 // An image of size 50x50. | 95 // An image of size 50x50. |
| 96 const char kJpegImage2Data[] = { |
| 97 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01
, 0x01, 0x00, 0x48, |
| 98 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0x43
, 0x01, 0xff, 0xff, |
| 103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xff, |
| 106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
, 0xff, 0xff, 0xc0, |
| 107 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11
, 0x01, 0x03, 0x11, |
| 108 0x01, 0xff, 0xc4, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, |
| 109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01
, 0x00, 0x00, 0x00, |
| 110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0xff, 0xc4, 0x00, |
| 111 0x15, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, |
| 112 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, |
| 113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xda, 0x00
, 0x0c, 0x03, 0x01, |
| 114 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00
, 0x00, 0x00, 0x00, |
| 115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x00, |
| 116 0x03, 0xff, 0xd9 |
| 117 }; |
| 118 const int kJpegImage2Width = 50; |
| 119 const int kJpegImage2Height = 50; |
| 120 |
| 121 // An image of size 50x50. |
| 90 static Vector<unsigned char> jpegImage2() | 122 static Vector<unsigned char> jpegImage2() |
| 91 { | 123 { |
| 92 Vector<unsigned char> jpeg; | 124 Vector<unsigned char> jpeg; |
| 93 | 125 jpeg.append(reinterpret_cast<const unsigned char*>(kJpegImage2Data), sizeof(
kJpegImage2Data)); |
| 94 static const unsigned char data[] = { | |
| 95 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, 0x48, | |
| 96 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 97 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 98 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00,
0x43, 0x01, 0xff, 0xff, | |
| 101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, | |
| 104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xc0, | |
| 105 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03, 0x01, 0x22, 0x00, 0x02,
0x11, 0x01, 0x03, 0x11, | |
| 106 0x01, 0xff, 0xc4, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, | |
| 107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10,
0x01, 0x00, 0x00, 0x00, | |
| 108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xc4, 0x00, | |
| 109 0x15, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, | |
| 110 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, | |
| 111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xda,
0x00, 0x0c, 0x03, 0x01, | |
| 112 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, | |
| 113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, | |
| 114 0x03, 0xff, 0xd9 | |
| 115 }; | |
| 116 | |
| 117 jpeg.append(data, sizeof(data)); | |
| 118 return jpeg; | 126 return jpeg; |
| 119 } | 127 } |
| 120 | 128 |
| 121 static Vector<unsigned char> svgImage() | 129 static Vector<unsigned char> svgImage() |
| 122 { | 130 { |
| 123 static const char data[] = | 131 static const char data[] = |
| 124 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\"
xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | 132 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\"
xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
| 125 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" | 133 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" |
| 126 "</svg>"; | 134 "</svg>"; |
| 127 | 135 |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 FetchRequest request(testURL, FetchInitiatorInfo()); | 638 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 631 ImageResource* cachedImage = ImageResource::fetch(request, fetcher); | 639 ImageResource* cachedImage = ImageResource::fetch(request, fetcher); |
| 632 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); | 640 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL); |
| 633 | 641 |
| 634 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(R
esourceResponse(testURL, "image/jpeg", 18, nullAtom, String())), nullptr); | 642 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(R
esourceResponse(testURL, "image/jpeg", 18, nullAtom, String())), nullptr); |
| 635 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18,
18); | 643 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18,
18); |
| 636 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); | 644 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); |
| 637 EXPECT_FALSE(cachedImage->isLoading()); | 645 EXPECT_FALSE(cachedImage->isLoading()); |
| 638 } | 646 } |
| 639 | 647 |
| 648 namespace { |
| 649 |
| 650 // A subrange of |kJpegImage2Data| from which the image dimensions can be de
coded. |
| 651 const size_t kRangeWithDimensionsLength = arraysize(kJpegImage2Data) - 10; |
| 652 static_assert(kRangeWithDimensionsLength < arraysize(kJpegImage2Data), "Samp
le image data is too short."); |
| 653 |
| 654 TEST(ImageResourceTest, EnsureLongTestSubrangeContainsDimensions) |
| 655 { |
| 656 RefPtr<blink::Image> image = BitmapImage::create(); |
| 657 EXPECT_EQ(Image::SizeAvailable, image->setData(SharedBuffer::create(kJpe
gImage2Data, kRangeWithDimensionsLength), true)); |
| 658 EXPECT_EQ(kJpegImage2Width, image->width()); |
| 659 EXPECT_EQ(kJpegImage2Height, image->height()); |
| 660 } |
| 661 |
| 662 // A subrange of |kJpegImage2Data| from which the image dimensions cannot be
decoded. |
| 663 const size_t kRangeWithoutDimensionsLength = 50; |
| 664 static_assert(kRangeWithoutDimensionsLength < arraysize(kJpegImage2Data), "S
ample image data is too short."); |
| 665 |
| 666 TEST(ImageResourceTest, EnsureShortTestSubrangeDoesNotContainDimensions) |
| 667 { |
| 668 RefPtr<blink::Image> image = BitmapImage::create(); |
| 669 EXPECT_EQ(Image::SizeUnavailable, image->setData(SharedBuffer::create(kJ
pegImage2Data, kRangeWithoutDimensionsLength), true)); |
| 670 } |
| 671 |
| 672 const char kPlaceholderForJpegImage2[] = "<?xml version=\"1.0\" encoding=\"U
TF-8\" standalone=\"no\"?>\n" |
| 673 "<svg xmlns=\"http://www.w3.org/200
0/svg\" width=\"50\" height=\"50\">\n" |
| 674 "<rect width=\"100%\" height=\"100%
\" style=\"fill:rgba(127,127,127,0.4);stroke-width:2;stroke:black\" />\n" |
| 675 "</svg>"; |
| 676 |
| 677 // A snapshot of some of the image-related state for an ImageResource. |
| 678 struct ImageResourceSnapshot { |
| 679 ImageResourceSnapshot() |
| 680 : isNull(true) |
| 681 , hasImage(false) |
| 682 , dataLength(0) |
| 683 { |
| 684 } |
| 685 |
| 686 ImageResourceSnapshot(ImageResource* image) |
| 687 : isNull(false) |
| 688 , hasImage(image->hasImage()) |
| 689 , dataLength(image->hasImage() && image->getImage()->data() ? image-
>getImage()->data()->size() : 0) |
| 690 , dimensions(image->hasImage() ? image->getImage()->size() : IntSize
()) |
| 691 { |
| 692 } |
| 693 |
| 694 ImageResourceSnapshot(bool hasImage, |
| 695 size_t dataLength, |
| 696 const IntSize& dimensions) |
| 697 : isNull(false) |
| 698 , hasImage(hasImage) |
| 699 , dataLength(dataLength) |
| 700 , dimensions(dimensions) |
| 701 { |
| 702 } |
| 703 |
| 704 bool operator==(const ImageResourceSnapshot& other) const |
| 705 { |
| 706 return isNull == other.isNull && hasImage == other.hasImage && dataL
ength == other.dataLength && dimensions == other.dimensions; |
| 707 } |
| 708 |
| 709 bool isNull; |
| 710 bool hasImage; |
| 711 size_t dataLength; |
| 712 IntSize dimensions; |
| 713 }; |
| 714 |
| 715 // Implement the stream output operator so that ImageResourceSnapshots can b
e |
| 716 // used with EXPECT_EQ. |
| 717 std::ostream& operator<<(std::ostream& out, const ImageResourceSnapshot& sna
pshot) |
| 718 { |
| 719 return out << "{isNull:" << snapshot.isNull << ", hasImage:" << snapshot
.hasImage << ", dataLength:" << snapshot.dataLength << ", dimensions:(" << snaps
hot.dimensions.width() << ';' << snapshot.dimensions.height() << ")}"; |
| 720 } |
| 721 |
| 722 // An ImageResourceObserver that keeps track of the number of times |
| 723 // imageChanged() and imageNotifyFinished() have been called, along with the |
| 724 // ImageResourceSnapshot from the most recent call of each. |
| 725 class TestImageResourceObserver : public GarbageCollectedFinalized<TestImage
ResourceObserver>, public ImageResourceObserver { |
| 726 USING_PRE_FINALIZER(TestImageResourceObserver, dispose); |
| 727 |
| 728 public: |
| 729 explicit TestImageResourceObserver(ImageResource* image) |
| 730 : m_image(image) |
| 731 , m_imageChangedCount(0) |
| 732 , m_imageNotifyFinishedCount(0) |
| 733 { |
| 734 image->addObserver(this); |
| 735 } |
| 736 |
| 737 ~TestImageResourceObserver() override; |
| 738 |
| 739 DECLARE_TRACE(); |
| 740 |
| 741 void dispose() |
| 742 { |
| 743 if (m_image) { |
| 744 m_image->removeObserver(this); |
| 745 m_image = nullptr; |
| 746 } |
| 747 } |
| 748 |
| 749 size_t imageChangedCount() const { return m_imageChangedCount; } |
| 750 const ImageResourceSnapshot& lastImageChangedSnapshot() const { return m
_lastImageChangedSnapshot; } |
| 751 size_t imageNotifyFinishedCount() const { return m_imageNotifyFinishedCo
unt; } |
| 752 const ImageResourceSnapshot& lastImageNotifyFinishedSnapshot() const { r
eturn m_lastImageNotifyFinishedSnapshot; } |
| 753 |
| 754 private: |
| 755 void imageChanged(ImageResource* image, const IntRect* = 0) override |
| 756 { |
| 757 EXPECT_EQ(m_image.get(), image); |
| 758 m_imageChangedCount++; |
| 759 m_lastImageChangedSnapshot = ImageResourceSnapshot(image); |
| 760 } |
| 761 |
| 762 void imageNotifyFinished(ImageResource* image) override |
| 763 { |
| 764 EXPECT_EQ(m_image.get(), image); |
| 765 m_imageNotifyFinishedCount++; |
| 766 m_lastImageNotifyFinishedSnapshot = ImageResourceSnapshot(image); |
| 767 } |
| 768 |
| 769 bool willRenderImage() override { return true; } |
| 770 |
| 771 String debugName() const override { return "TestImageResourceObserver";
} |
| 772 |
| 773 Member<ImageResource> m_image; |
| 774 |
| 775 size_t m_imageChangedCount; |
| 776 ImageResourceSnapshot m_lastImageChangedSnapshot; |
| 777 size_t m_imageNotifyFinishedCount; |
| 778 ImageResourceSnapshot m_lastImageNotifyFinishedSnapshot; |
| 779 }; |
| 780 |
| 781 TestImageResourceObserver::~TestImageResourceObserver() {} |
| 782 |
| 783 DEFINE_TRACE(TestImageResourceObserver) |
| 784 { |
| 785 visitor->trace(m_image); |
| 786 } |
| 787 |
| 788 // Convenience class for registering and unregistering a mock load for a tes
t |
| 789 // URL. |
| 790 class ScopedRegisteredURL { |
| 791 public: |
| 792 ScopedRegisteredURL(const KURL& url) |
| 793 : m_url(url) |
| 794 { |
| 795 URLTestHelpers::registerMockedURLLoad(url, "cancelTest.html", "text/
html"); |
| 796 } |
| 797 |
| 798 ~ScopedRegisteredURL() |
| 799 { |
| 800 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url)
; |
| 801 } |
| 802 |
| 803 private: |
| 804 KURL m_url; |
| 805 }; |
| 806 |
| 807 // Implement the stream output operator for WebCachePolicy so that it can be |
| 808 // used with EXPECT_EQ. |
| 809 std::ostream& operator<<(std::ostream& out, WebCachePolicy webCachePolicy) |
| 810 { |
| 811 return out << static_cast<int>(webCachePolicy); |
| 812 } |
| 813 |
| 814 static AtomicString buildContentRangeHeader(size_t rangeLength, size_t total
Length) |
| 815 { |
| 816 return AtomicString(String("bytes 0-" + String::number(rangeLength - 1)
+ "/" + String::number(totalLength))); |
| 817 } |
| 818 |
| 819 // Construct an "image/jpeg" response with the following properties. |
| 820 static ResourceResponse buildImageResponse(const KURL& url, size_t length, i
nt statusCode, const AtomicString& contentRangeHeader, bool wasCached) |
| 821 { |
| 822 ResourceResponse response(url, "image/jpeg", static_cast<long long>(leng
th), nullAtom, String()); |
| 823 response.setHTTPStatusCode(statusCode); |
| 824 if (!contentRangeHeader.isNull()) |
| 825 response.setHTTPHeaderField("content-range", contentRangeHeader); |
| 826 response.setWasCached(wasCached); |
| 827 return response; |
| 828 } |
| 829 |
| 830 // Use the |image|'s attached ResourceLoader to simulate the image receiving
a response. |
| 831 static void simulateImageLoad(ImageResource* image, const ResourceResponse&
response, const char* data, size_t length) |
| 832 { |
| 833 const int intLength = safeCast<int>(length); |
| 834 ResourceLoader* loader = image->loader(); |
| 835 loader->didReceiveResponse(nullptr, WrappedResourceResponse(response)); |
| 836 if (image->loader() == loader && data && length) { |
| 837 loader->didReceiveData(nullptr, data, intLength, intLength, intLengt
h); |
| 838 } |
| 839 if (image->loader() == loader) { |
| 840 loader->didFinishLoading(nullptr, 0.0, intLength); |
| 841 } |
| 842 } |
| 843 |
| 844 // Use the |image|'s attached ResourceLoader to simulate the image encounter
ing an error. |
| 845 static void simulateImageFailure(ImageResource* image, const ResourceError&
error) |
| 846 { |
| 847 ResourceLoader* loader = image->loader(); |
| 848 loader->didFail(nullptr, error); |
| 849 if (image->loader() == loader) { |
| 850 loader->didFinishLoading(nullptr, 0.0, 0); |
| 851 } |
| 852 } |
| 853 |
| 854 static void expectOriginalRequestIsForFullImage(ImageResource* image, int te
stId = 0) |
| 855 { |
| 856 EXPECT_EQ(nullAtom, image->getOriginalResourceRequest().httpHeaderField(
"range")) << testId; |
| 857 EXPECT_EQ(WebCachePolicy::UseProtocolCachePolicy, image->getOriginalReso
urceRequest().getCachePolicy()) << testId; |
| 858 } |
| 859 |
| 860 static void expectLoadingFullImage(ImageResource* image, int testId = 0) |
| 861 { |
| 862 EXPECT_TRUE(image->isLoading()) << testId; |
| 863 EXPECT_FALSE(image->isPlaceholder()) << testId; |
| 864 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")) <
< testId; |
| 865 EXPECT_EQ(WebCachePolicy::UseProtocolCachePolicy, image->resourceRequest
().getCachePolicy()) << testId; |
| 866 |
| 867 expectOriginalRequestIsForFullImage(image, testId); |
| 868 } |
| 869 |
| 870 static void expectLoadingPlaceholder(ImageResource* image, int testId = 0) |
| 871 { |
| 872 EXPECT_TRUE(image->isLoading()) << testId; |
| 873 EXPECT_TRUE(image->isPlaceholder()) << testId; |
| 874 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("rang
e")) << testId; |
| 875 EXPECT_EQ(WebCachePolicy::UseProtocolCachePolicy, image->resourceRequest
().getCachePolicy()) << testId; |
| 876 |
| 877 expectOriginalRequestIsForFullImage(image, testId); |
| 878 } |
| 879 |
| 880 static void expectLoadingFullImageFromCache(ImageResource* image, int testId
= 0) |
| 881 { |
| 882 EXPECT_TRUE(image->isLoading()) << testId; |
| 883 EXPECT_TRUE(image->isPlaceholder()) << testId; |
| 884 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")) <
< testId; |
| 885 EXPECT_EQ(WebCachePolicy::ReturnCacheDataDontLoad, image->resourceReques
t().getCachePolicy()) << testId; |
| 886 |
| 887 expectOriginalRequestIsForFullImage(image, testId); |
| 888 } |
| 889 |
| 890 static void expectLoadingFullImageBypassingCache(ImageResource* image, int t
estId = 0) |
| 891 { |
| 892 EXPECT_TRUE(image->isLoading()) << testId; |
| 893 EXPECT_FALSE(image->isPlaceholder()) << testId; |
| 894 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")) <
< testId; |
| 895 EXPECT_EQ(WebCachePolicy::BypassingCache, image->resourceRequest().getCa
chePolicy()) << testId; |
| 896 |
| 897 expectOriginalRequestIsForFullImage(image, testId); |
| 898 } |
| 899 |
| 900 // Ensure that the current state of |image| and the most recent notification
s |
| 901 // sent to |observer| all match the given |expectedSnapshot|. |
| 902 static void expectMatchesSnapshot(ImageResource* image, const TestImageResou
rceObserver& observer, const ImageResourceSnapshot& expectedSnapshot, int testId
= 0) |
| 903 { |
| 904 EXPECT_EQ(expectedSnapshot, ImageResourceSnapshot(image)) << testId; |
| 905 EXPECT_LT(0U, observer.imageChangedCount()) << testId; |
| 906 EXPECT_EQ(expectedSnapshot, observer.lastImageChangedSnapshot()) << test
Id; |
| 907 EXPECT_EQ(1U, observer.imageNotifyFinishedCount()) << testId; |
| 908 EXPECT_EQ(expectedSnapshot, observer.lastImageNotifyFinishedSnapshot())
<< testId; |
| 909 } |
| 910 |
| 911 } // namespace |
| 912 |
| 913 TEST(ImageResourceTest, FetchImage) |
| 914 { |
| 915 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 916 ScopedRegisteredURL scopedTestLoad(testURL); |
| 917 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 918 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create())); |
| 919 expectLoadingFullImage(image); |
| 920 |
| 921 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 922 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 923 |
| 924 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 925 EXPECT_FALSE(image->isPlaceholder()); |
| 926 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arraysiz
e(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 927 } |
| 928 |
| 929 TEST(ImageResourceTest, FetchPlaceholderNotCached) |
| 930 { |
| 931 AtomicString contentRangeHeaderOptions[] = { |
| 932 buildContentRangeHeader(kRangeWithDimensionsLength, arraysize(kJpegImage
2Data)), |
| 933 "bogus content range", |
| 934 AtomicString(String("bytes 1-" + String::number(kRangeWithDimensionsLeng
th) + "/" + String::number(arraysize(kJpegImage2Data)))), |
| 935 "bytes ", |
| 936 "", |
| 937 }; |
| 938 for (const AtomicString& contentRange : contentRangeHeaderOptions) { |
| 939 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 940 ScopedRegisteredURL scopedTestLoad(testURL); |
| 941 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 942 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 943 expectLoadingPlaceholder(image); |
| 944 |
| 945 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 946 simulateImageLoad(image, buildImageResponse(testURL, kRangeWithDimension
sLength, 206, contentRange, false), kJpegImage2Data, kRangeWithDimensionsLength)
; |
| 947 |
| 948 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 949 EXPECT_TRUE(image->isPlaceholder()); |
| 950 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, strl
en(kPlaceholderForJpegImage2), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 951 } |
| 952 } |
| 953 |
| 954 TEST(ImageResourceTest, FetchPlaceholderThenFetchFullAfterLoading) |
| 955 { |
| 956 const KURL testURL(ParsedURLString, "http://www.test.com/test_placeholder_im
age.jpg"); |
| 957 ScopedRegisteredURL scopedTestLoad(testURL); |
| 958 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc
hContext::create()); |
| 959 FetchRequest allowPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 960 ImageResource* image = ImageResource::fetch(allowPlaceholderRequest, fetcher
, ImageResource::PlaceholderRequestType::AllowPlaceholder); |
| 961 expectLoadingPlaceholder(image); |
| 962 |
| 963 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 964 simulateImageLoad(image, buildImageResponse(testURL, kRangeWithDimensionsLen
gth, 206, buildContentRangeHeader(kRangeWithDimensionsLength, arraysize(kJpegIma
ge2Data)), false), kJpegImage2Data, kRangeWithDimensionsLength); |
| 965 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 966 EXPECT_TRUE(image->isPlaceholder()); |
| 967 |
| 968 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, strlen(k
PlaceholderForJpegImage2), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 969 const ImageResourceSnapshot placeholderSnapshot(image); |
| 970 EXPECT_EQ(1U, observer->imageNotifyFinishedCount()); |
| 971 EXPECT_EQ(placeholderSnapshot, observer->lastImageNotifyFinishedSnapshot()); |
| 972 |
| 973 FetchRequest disallowPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 974 ImageResource* fullImage = ImageResource::fetch(disallowPlaceholderRequest,
fetcher, ImageResource::PlaceholderRequestType::DisallowPlaceholder); |
| 975 EXPECT_EQ(image, fullImage); |
| 976 expectLoadingFullImage(image); |
| 977 |
| 978 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 979 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 980 |
| 981 const ImageResourceSnapshot expectedSnapshot(true, arraysize(kJpegImage2Data
), IntSize(kJpegImage2Width, kJpegImage2Height)); |
| 982 EXPECT_EQ(expectedSnapshot, ImageResourceSnapshot(image)); |
| 983 EXPECT_LT(0U, observer->imageChangedCount()); |
| 984 EXPECT_EQ(expectedSnapshot, observer->lastImageChangedSnapshot()); |
| 985 EXPECT_EQ(1U, observer->imageNotifyFinishedCount()); |
| 986 EXPECT_EQ(placeholderSnapshot, observer->lastImageNotifyFinishedSnapshot()); |
| 987 } |
| 988 |
| 989 TEST(ImageResourceTest, FetchPlaceholderThenFetchFullWhileLoading) |
| 990 { |
| 991 const KURL testURL(ParsedURLString, "http://www.test.com/test_placeholder_im
age.jpg"); |
| 992 ScopedRegisteredURL scopedTestLoad(testURL); |
| 993 FetchRequest allowPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 994 ResourceFetcher* fetcher = ResourceFetcher::create(ImageResourceTestMockFetc
hContext::create()); |
| 995 ImageResource* image = ImageResource::fetch(allowPlaceholderRequest, fetcher
, ImageResource::PlaceholderRequestType::AllowPlaceholder); |
| 996 expectLoadingPlaceholder(image); |
| 997 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 998 |
| 999 FetchRequest disallowPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1000 ImageResource* fullImage = ImageResource::fetch(disallowPlaceholderRequest,
fetcher, ImageResource::PlaceholderRequestType::DisallowPlaceholder); |
| 1001 EXPECT_EQ(image, fullImage); |
| 1002 expectLoadingFullImage(image); |
| 1003 |
| 1004 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1005 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1006 EXPECT_FALSE(image->isPlaceholder()); |
| 1007 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arraysiz
e(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 1008 } |
| 1009 |
| 1010 TEST(ImageResourceTest, FetchPlaceholderWith204) |
| 1011 { |
| 1012 const bool wasCachedOptions[] = { false, true }; |
| 1013 for (const auto& wasCached : wasCachedOptions) { |
| 1014 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1015 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1016 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1017 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 1018 expectLoadingPlaceholder(image, &wasCached - wasCachedOptions); |
| 1019 |
| 1020 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 1021 simulateImageLoad(image, buildImageResponse(testURL, 0, 204, nullAtom, w
asCached), nullptr, 0); |
| 1022 |
| 1023 EXPECT_EQ(Resource::DecodeError, image->getStatus()) << (&wasCached - wa
sCachedOptions); |
| 1024 EXPECT_FALSE(image->isPlaceholder()) << (&wasCached - wasCachedOptions); |
| 1025 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(false, 0,
IntSize()), &wasCached - wasCachedOptions); |
| 1026 } |
| 1027 } |
| 1028 |
| 1029 TEST(ImageResourceTest, FetchPlaceholderWithEntireResourceForRange) |
| 1030 { |
| 1031 const bool wasCachedOptions[] = { false, true }; |
| 1032 for (const auto& wasCached : wasCachedOptions) { |
| 1033 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1034 const ResourceResponse fullResponses[] = { |
| 1035 buildImageResponse(testURL, arraysize(kJpegImage2Data), 206, buildCo
ntentRangeHeader(arraysize(kJpegImage2Data), arraysize(kJpegImage2Data)), wasCac
hed), |
| 1036 buildImageResponse(testURL, arraysize(kJpegImage2Data), 200, nullAto
m, wasCached), |
| 1037 buildImageResponse(testURL, arraysize(kJpegImage2Data), 404, nullAto
m, wasCached), |
| 1038 }; |
| 1039 for (const ResourceResponse& response : fullResponses) { |
| 1040 int testId = (&wasCached - wasCachedOptions) * arraysize(fullRespons
es) + (&response - fullResponses); |
| 1041 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1042 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1043 ImageResource* image = ImageResource::fetch(request, ResourceFetcher
::create(ImageResourceTestMockFetchContext::create()), ImageResource::Placeholde
rRequestType::AllowPlaceholder); |
| 1044 expectLoadingPlaceholder(image, testId); |
| 1045 |
| 1046 TestImageResourceObserver* observer = new TestImageResourceObserver(
image); |
| 1047 simulateImageLoad(image, response, kJpegImage2Data, arraysize(kJpegI
mage2Data)); |
| 1048 |
| 1049 EXPECT_EQ(Resource::Cached, image->getStatus()) << testId; |
| 1050 EXPECT_FALSE(image->isPlaceholder()) << testId; |
| 1051 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true,
arraysize(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height)), testI
d); |
| 1052 } |
| 1053 } |
| 1054 } |
| 1055 |
| 1056 TEST(ImageResourceTest, FetchPlaceholderPartiallyCached) |
| 1057 { |
| 1058 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1059 static const char kBadResponseData[] = "bad image response"; |
| 1060 |
| 1061 const struct { |
| 1062 ResourceError cacheError; |
| 1063 ResourceResponse cacheResponse; |
| 1064 const char* imageData; |
| 1065 size_t imageDataLength; |
| 1066 } tests[] = { |
| 1067 { ResourceError("net", -400, testURL, "Cache miss"), ResourceResponse(),
nullptr, 0 }, |
| 1068 { ResourceError(), buildImageResponse(testURL, arraysize(kBadResponseDat
a), 200, nullAtom, true), kBadResponseData, arraysize(kBadResponseData) }, |
| 1069 { ResourceError(), buildImageResponse(testURL, kRangeWithDimensionsLengt
h, 206, buildContentRangeHeader(kRangeWithDimensionsLength, arraysize(kJpegImage
2Data)), true), kJpegImage2Data, kRangeWithDimensionsLength }, |
| 1070 }; |
| 1071 for (const auto& test : tests) { |
| 1072 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1073 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1074 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 1075 expectLoadingPlaceholder(image, &test - tests); |
| 1076 |
| 1077 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 1078 simulateImageLoad(image, buildImageResponse(testURL, kRangeWithDimension
sLength, 206, buildContentRangeHeader(kRangeWithDimensionsLength, arraysize(kJpe
gImage2Data)), true), kJpegImage2Data, kRangeWithDimensionsLength); |
| 1079 expectLoadingFullImageFromCache(image, &test - tests); |
| 1080 |
| 1081 if (test.cacheError.isNull()) { |
| 1082 simulateImageLoad(image, test.cacheResponse, test.imageData, test.im
ageDataLength); |
| 1083 } else { |
| 1084 simulateImageFailure(image, test.cacheError); |
| 1085 } |
| 1086 |
| 1087 EXPECT_EQ(Resource::Cached, image->getStatus()) << (&test - tests); |
| 1088 EXPECT_TRUE(image->isPlaceholder()) << (&test - tests); |
| 1089 EXPECT_FALSE(image->willPaintBrokenImage()) << (&test - tests); |
| 1090 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, strl
en(kPlaceholderForJpegImage2), IntSize(kJpegImage2Width, kJpegImage2Height)), &t
est - tests); |
| 1091 } |
| 1092 } |
| 1093 |
| 1094 TEST(ImageResourceTest, FetchPlaceholderFullyCached) |
| 1095 { |
| 1096 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1097 static const char kBadResponseData[] = "bad image response"; |
| 1098 |
| 1099 const struct { |
| 1100 ResourceResponse rangeResponse; |
| 1101 const char* imageData; |
| 1102 size_t imageDataLength; |
| 1103 } tests[] = { |
| 1104 { buildImageResponse(testURL, kRangeWithDimensionsLength, 206, buildCont
entRangeHeader(kRangeWithDimensionsLength, arraysize(kJpegImage2Data)), true), k
JpegImage2Data, kRangeWithDimensionsLength }, |
| 1105 { buildImageResponse(testURL, kRangeWithoutDimensionsLength, 206, buildC
ontentRangeHeader(kRangeWithoutDimensionsLength, arraysize(kJpegImage2Data)), tr
ue), kJpegImage2Data, kRangeWithoutDimensionsLength }, |
| 1106 { buildImageResponse(testURL, arraysize(kBadResponseData), 206, buildCon
tentRangeHeader(arraysize(kBadResponseData), arraysize(kBadResponseData) + 10),
true), kBadResponseData, arraysize(kBadResponseData) }, |
| 1107 }; |
| 1108 for (const auto& test : tests) { |
| 1109 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1110 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1111 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 1112 expectLoadingPlaceholder(image, &test - tests); |
| 1113 |
| 1114 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 1115 simulateImageLoad(image, test.rangeResponse, test.imageData, test.imageD
ataLength); |
| 1116 expectLoadingFullImageFromCache(image, &test - tests); |
| 1117 |
| 1118 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImag
e2Data), 200, nullAtom, true), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1119 EXPECT_EQ(Resource::Cached, image->getStatus()) << (&test - tests); |
| 1120 EXPECT_FALSE(image->isPlaceholder()) << (&test - tests); |
| 1121 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arra
ysize(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height)), &test - t
ests); |
| 1122 } |
| 1123 } |
| 1124 |
| 1125 TEST(ImageResourceTest, FetchPlaceholderFallbackToFullWithoutCacheCheck) |
| 1126 { |
| 1127 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1128 static const char kBadResponseData[] = "bad image response"; |
| 1129 |
| 1130 const struct { |
| 1131 ResourceResponse rangeResponse; |
| 1132 const char* imageData; |
| 1133 size_t imageDataLength; |
| 1134 } tests[] = { |
| 1135 { buildImageResponse(testURL, kRangeWithoutDimensionsLength, 206, buildC
ontentRangeHeader(kRangeWithoutDimensionsLength, arraysize(kJpegImage2Data)), fa
lse), kJpegImage2Data, kRangeWithoutDimensionsLength }, |
| 1136 { buildImageResponse(testURL, arraysize(kBadResponseData), 206, buildCon
tentRangeHeader(arraysize(kBadResponseData), arraysize(kBadResponseData) + 10),
false), kBadResponseData, arraysize(kBadResponseData) }, |
| 1137 { buildImageResponse(testURL, arraysize(kBadResponseData), 200, nullAtom
, true), kBadResponseData, arraysize(kBadResponseData) }, |
| 1138 }; |
| 1139 for (const auto& test : tests) { |
| 1140 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1141 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1142 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 1143 expectLoadingPlaceholder(image, &test - tests); |
| 1144 |
| 1145 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 1146 simulateImageLoad(image, test.rangeResponse, test.imageData, test.imageD
ataLength); |
| 1147 expectLoadingFullImageBypassingCache(image, &test - tests); |
| 1148 |
| 1149 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImag
e2Data), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1150 EXPECT_EQ(Resource::Cached, image->getStatus()) << (&test - tests); |
| 1151 EXPECT_FALSE(image->isPlaceholder()) << (&test - tests); |
| 1152 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arra
ysize(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height)), &test - t
ests); |
| 1153 } |
| 1154 } |
| 1155 |
| 1156 TEST(ImageResourceTest, FetchPlaceholderFallbackToFullWithCacheCheck) |
| 1157 { |
| 1158 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1159 static const char kBadResponseData[] = "bad image response"; |
| 1160 |
| 1161 const struct { |
| 1162 ResourceResponse rangeResponse; |
| 1163 const char* imageData; |
| 1164 size_t imageDataLength; |
| 1165 } tests[] = { |
| 1166 { buildImageResponse(testURL, kRangeWithoutDimensionsLength, 206, buildC
ontentRangeHeader(kRangeWithoutDimensionsLength, arraysize(kJpegImage2Data)), tr
ue), kJpegImage2Data, kRangeWithoutDimensionsLength }, |
| 1167 { buildImageResponse(testURL, arraysize(kBadResponseData), 206, buildCon
tentRangeHeader(arraysize(kBadResponseData), arraysize(kBadResponseData) + 10),
true), kBadResponseData, arraysize(kBadResponseData) }, |
| 1168 }; |
| 1169 for (const auto& test : tests) { |
| 1170 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1171 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1172 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::cr
eate(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderReq
uestType::AllowPlaceholder); |
| 1173 expectLoadingPlaceholder(image, &test - tests); |
| 1174 |
| 1175 TestImageResourceObserver* observer = new TestImageResourceObserver(imag
e); |
| 1176 simulateImageLoad(image, test.rangeResponse, test.imageData, test.imageD
ataLength); |
| 1177 expectLoadingFullImageFromCache(image, &test - tests); |
| 1178 |
| 1179 simulateImageFailure(image, ResourceError("net", -400, testURL, "Cache m
iss")); |
| 1180 expectLoadingFullImageBypassingCache(image, &test - tests); |
| 1181 |
| 1182 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImag
e2Data), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1183 EXPECT_EQ(Resource::Cached, image->getStatus()) << (&test - tests); |
| 1184 EXPECT_FALSE(image->isPlaceholder()) << (&test - tests); |
| 1185 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arra
ysize(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height)), &test - t
ests); |
| 1186 } |
| 1187 } |
| 1188 |
| 1189 TEST(ImageResourceTest, FetchPlaceholderWithForcedCacheAndCachedRange) |
| 1190 { |
| 1191 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1192 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1193 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1194 request.mutableResourceRequest().setCachePolicy(WebCachePolicy::ReturnCacheD
ataDontLoad); |
| 1195 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderRequest
Type::AllowPlaceholder); |
| 1196 EXPECT_TRUE(image->isLoading()); |
| 1197 EXPECT_TRUE(image->isPlaceholder()); |
| 1198 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"))
; |
| 1199 EXPECT_EQ(WebCachePolicy::ReturnCacheDataDontLoad, image->resourceRequest().
getCachePolicy()); |
| 1200 |
| 1201 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 1202 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 206, buildContentRangeHeader(kRangeWithDimensionsLength, arraysize(kJpegIma
ge2Data)), true), kJpegImage2Data, kRangeWithDimensionsLength); |
| 1203 EXPECT_TRUE(image->isLoading()); |
| 1204 EXPECT_TRUE(image->isPlaceholder()); |
| 1205 EXPECT_EQ(WebCachePolicy::ReturnCacheDataDontLoad, image->resourceRequest().
getCachePolicy()); |
| 1206 |
| 1207 simulateImageFailure(image, ResourceError("net", -400, testURL, "Cache miss"
)); |
| 1208 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1209 EXPECT_TRUE(image->isPlaceholder()); |
| 1210 |
| 1211 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, strlen(k
PlaceholderForJpegImage2), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 1212 } |
| 1213 |
| 1214 TEST(ImageResourceTest, FetchPlaceholderWithForcedCacheAndNoCachedRange) |
| 1215 { |
| 1216 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1217 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1218 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1219 request.mutableResourceRequest().setCachePolicy(WebCachePolicy::ReturnCacheD
ataDontLoad); |
| 1220 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderRequest
Type::AllowPlaceholder); |
| 1221 EXPECT_TRUE(image->isLoading()); |
| 1222 EXPECT_TRUE(image->isPlaceholder()); |
| 1223 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"))
; |
| 1224 EXPECT_EQ(WebCachePolicy::ReturnCacheDataDontLoad, image->resourceRequest().
getCachePolicy()); |
| 1225 |
| 1226 simulateImageFailure(image, ResourceError("net", -400, testURL, "Cache miss"
)); |
| 1227 EXPECT_FALSE(image->isLoading()); |
| 1228 EXPECT_TRUE(image->willPaintBrokenImage()); |
| 1229 EXPECT_FALSE(image->isPlaceholder()); |
| 1230 } |
| 1231 |
| 1232 TEST(ImageResourceTest, FetchDataURIAvoidsPlaceholder) |
| 1233 { |
| 1234 const KURL testURL(ParsedURLString, String("data:image/jpeg;base64," + base6
4Encode(kJpegImage2Data, arraysize(kJpegImage2Data)))); |
| 1235 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1236 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderRequest
Type::AllowPlaceholder); |
| 1237 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1238 EXPECT_FALSE(image->isPlaceholder()); |
| 1239 |
| 1240 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 1241 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arraysiz
e(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 1242 } |
| 1243 |
| 1244 TEST(ImageResourceTest, FetchWithPreexistingRangeHeaderAvoidsPlaceholder) |
| 1245 { |
| 1246 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1247 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1248 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1249 const AtomicString testRange(String("bytes=0-" + String::number(arraysize(kJ
pegImage2Data) - 1))); |
| 1250 request.mutableResourceRequest().setHTTPHeaderField("range", testRange); |
| 1251 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderRequest
Type::AllowPlaceholder); |
| 1252 EXPECT_TRUE(image->isLoading()); |
| 1253 EXPECT_FALSE(image->isPlaceholder()); |
| 1254 EXPECT_EQ(testRange, image->resourceRequest().httpHeaderField("range")); |
| 1255 |
| 1256 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 1257 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 206, buildContentRangeHeader(arraysize(kJpegImage2Data), arraysize(kJpegIma
ge2Data) + 10), false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1258 |
| 1259 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1260 EXPECT_FALSE(image->isPlaceholder()); |
| 1261 |
| 1262 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arraysiz
e(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 1263 } |
| 1264 |
| 1265 TEST(ImageResourceTest, FetchWithPostRequestAvoidsPlaceholder) |
| 1266 { |
| 1267 const KURL testURL(ParsedURLString, "http://www.test.com/image.jpg"); |
| 1268 ScopedRegisteredURL scopedTestLoad(testURL); |
| 1269 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1270 request.mutableResourceRequest().setHTTPMethod("POST"); |
| 1271 ImageResource* image = ImageResource::fetch(request, ResourceFetcher::create
(ImageResourceTestMockFetchContext::create()), ImageResource::PlaceholderRequest
Type::AllowPlaceholder); |
| 1272 expectLoadingFullImage(image); |
| 1273 |
| 1274 TestImageResourceObserver* observer = new TestImageResourceObserver(image); |
| 1275 simulateImageLoad(image, buildImageResponse(testURL, arraysize(kJpegImage2Da
ta), 200, nullAtom, false), kJpegImage2Data, arraysize(kJpegImage2Data)); |
| 1276 |
| 1277 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1278 EXPECT_FALSE(image->isPlaceholder()); |
| 1279 |
| 1280 expectMatchesSnapshot(image, *observer, ImageResourceSnapshot(true, arraysiz
e(kJpegImage2Data), IntSize(kJpegImage2Width, kJpegImage2Height))); |
| 1281 } |
| 1282 |
| 640 } // namespace blink | 1283 } // namespace blink |
| OLD | NEW |