| 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 21 matching lines...) Expand all Loading... |
| 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/MockResourceClients.h" | 36 #include "core/fetch/MockResourceClients.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 "platform/SharedBuffer.h" | 40 #include "platform/SharedBuffer.h" |
| 41 #include "platform/exported/WrappedResourceResponse.h" | 41 #include "platform/exported/WrappedResourceResponse.h" |
| 42 #include "platform/graphics/BitmapImage.h" |
| 42 #include "platform/graphics/Image.h" | 43 #include "platform/graphics/Image.h" |
| 43 #include "platform/scheduler/test/fake_web_task_runner.h" | 44 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 44 #include "platform/testing/URLTestHelpers.h" | 45 #include "platform/testing/URLTestHelpers.h" |
| 45 #include "platform/testing/UnitTestHelpers.h" | 46 #include "platform/testing/UnitTestHelpers.h" |
| 46 #include "public/platform/Platform.h" | 47 #include "public/platform/Platform.h" |
| 48 #include "public/platform/WebCachePolicy.h" |
| 47 #include "public/platform/WebURL.h" | 49 #include "public/platform/WebURL.h" |
| 48 #include "public/platform/WebURLLoaderMockFactory.h" | 50 #include "public/platform/WebURLLoaderMockFactory.h" |
| 49 #include "public/platform/WebURLResponse.h" | 51 #include "public/platform/WebURLResponse.h" |
| 50 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
| 51 #include "wtf/PtrUtil.h" | 53 #include "wtf/PtrUtil.h" |
| 54 #include "wtf/text/Base64.h" |
| 52 #include <memory> | 55 #include <memory> |
| 53 | 56 |
| 54 namespace blink { | 57 namespace blink { |
| 55 | 58 |
| 56 namespace { | 59 namespace { |
| 57 | 60 |
| 58 // An image of size 1x1. | 61 // An image of size 1x1. |
| 59 static Vector<unsigned char> jpegImage() { | 62 const unsigned char kJpegImage[] = { |
| 60 Vector<unsigned char> jpeg; | 63 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, |
| 64 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, |
| 65 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, |
| 66 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x05, 0x03, |
| 67 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, |
| 68 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, |
| 69 0x11, 0x0f, 0x12, 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, |
| 70 0x13, 0x14, 0x1a, 0x15, 0x11, 0x11, 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, |
| 71 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22, 0x24, 0x22, 0x1e, 0x24, 0x1c, 0x1e, |
| 72 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05, 0x05, 0x07, 0x06, |
| 73 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e, |
| 74 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, |
| 75 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, |
| 76 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, |
| 77 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff, |
| 78 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00, |
| 79 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, |
| 80 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 81 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01, 0x00, |
| 82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, |
| 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, |
| 87 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, |
| 88 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; |
| 61 | 89 |
| 62 static const unsigned char data[] = { | 90 const size_t kJpegImageSubrangeWithDimensionsLength = sizeof(kJpegImage) - 1; |
| 63 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, | |
| 64 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, | |
| 65 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, | |
| 66 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x05, 0x03, | |
| 67 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, | |
| 68 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, | |
| 69 0x11, 0x0f, 0x12, 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, | |
| 70 0x13, 0x14, 0x1a, 0x15, 0x11, 0x11, 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, | |
| 71 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22, 0x24, 0x22, 0x1e, 0x24, 0x1c, 0x1e, | |
| 72 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05, 0x05, 0x07, 0x06, | |
| 73 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e, | |
| 74 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, | |
| 75 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, | |
| 76 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, | |
| 77 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff, | |
| 78 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00, | |
| 79 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, | |
| 80 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 81 0x00, 0x00, 0x00, 0x00, 0x08, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01, 0x00, | |
| 82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 83 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00, | |
| 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 85 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, | |
| 87 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, | |
| 88 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; | |
| 89 | 91 |
| 90 jpeg.append(data, sizeof(data)); | 92 // Ensure that the image decoder can determine the dimensions of kJpegImage from |
| 91 return jpeg; | 93 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test |
| 94 // fails, then the test data here probably needs to be updated. |
| 95 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) { |
| 96 RefPtr<Image> image = BitmapImage::create(); |
| 97 EXPECT_EQ( |
| 98 Image::SizeAvailable, |
| 99 image->setData(SharedBuffer::create( |
| 100 kJpegImage, kJpegImageSubrangeWithDimensionsLength), |
| 101 true)); |
| 102 EXPECT_TRUE(image->isBitmapImage()); |
| 103 EXPECT_EQ(1, image->width()); |
| 104 EXPECT_EQ(1, image->height()); |
| 92 } | 105 } |
| 93 | 106 |
| 94 // An image of size 50x50. | 107 // An image of size 50x50. |
| 95 static Vector<unsigned char> jpegImage2() { | 108 const unsigned char kJpegImage2[] = { |
| 96 Vector<unsigned char> jpeg; | 109 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, |
| 110 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, |
| 111 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 115 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0x43, 0x01, 0xff, 0xff, |
| 117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 122 0xff, 0xff, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03, |
| 123 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, |
| 124 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10, |
| 126 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 127 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, |
| 128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 129 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, |
| 130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, |
| 132 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 134 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; |
| 97 | 135 |
| 98 static const unsigned char data[] = { | 136 const char kSvgImage[] = |
| 99 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, | 137 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " |
| 100 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, | 138 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
| 101 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | 139 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" |
| 102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | 140 "</svg>"; |
| 103 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, | |
| 105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0x43, 0x01, 0xff, 0xff, | |
| 107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 110 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 112 0xff, 0xff, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03, | |
| 113 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, | |
| 114 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10, | |
| 116 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 117 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01, | |
| 118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 119 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, | |
| 120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 121 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, | |
| 122 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 124 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9}; | |
| 125 | 141 |
| 126 jpeg.append(data, sizeof(data)); | 142 const char kSvgImage2[] = |
| 127 return jpeg; | 143 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " |
| 128 } | 144 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" |
| 129 | 145 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" |
| 130 static Vector<unsigned char> svgImage() { | 146 "</svg>"; |
| 131 static const char data[] = | |
| 132 "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" " | |
| 133 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | |
| 134 "<rect x=\"0\" y=\"0\" width=\"100px\" height=\"100px\" fill=\"red\"/>" | |
| 135 "</svg>"; | |
| 136 | |
| 137 Vector<unsigned char> svg; | |
| 138 svg.append(data, strlen(data)); | |
| 139 return svg; | |
| 140 } | |
| 141 | |
| 142 static Vector<unsigned char> svgImage2() { | |
| 143 static const char data[] = | |
| 144 "<svg width=\"300\" height=\"300\" xmlns=\"http://www.w3.org/2000/svg\" " | |
| 145 "xmlns:xlink=\"http://www.w3.org/1999/xlink\">" | |
| 146 "<rect x=\"0\" y=\"0\" width=\"200px\" height=\"200px\" fill=\"green\"/>" | |
| 147 "</svg>"; | |
| 148 | |
| 149 Vector<unsigned char> svg; | |
| 150 svg.append(data, strlen(data)); | |
| 151 return svg; | |
| 152 } | |
| 153 | 147 |
| 154 void receiveResponse(ImageResource* imageResource, | 148 void receiveResponse(ImageResource* imageResource, |
| 155 const KURL& url, | 149 const KURL& url, |
| 156 const AtomicString& mimeType, | 150 const AtomicString& mimeType, |
| 157 const Vector<unsigned char>& data) { | 151 const char* data, |
| 152 size_t dataSize) { |
| 158 ResourceResponse response; | 153 ResourceResponse response; |
| 159 response.setURL(url); | 154 response.setURL(url); |
| 160 response.setHTTPStatusCode(200); | 155 response.setHTTPStatusCode(200); |
| 161 response.setMimeType(mimeType); | 156 response.setMimeType(mimeType); |
| 162 imageResource->responseReceived(response, nullptr); | 157 imageResource->responseReceived(response, nullptr); |
| 163 imageResource->appendData(reinterpret_cast<const char*>(data.data()), | 158 imageResource->appendData(data, dataSize); |
| 164 data.size()); | |
| 165 imageResource->finish(); | 159 imageResource->finish(); |
| 166 } | 160 } |
| 167 | 161 |
| 168 class ImageResourceTestMockFetchContext : public FetchContext { | 162 class ImageResourceTestMockFetchContext : public FetchContext { |
| 169 public: | 163 public: |
| 170 static ImageResourceTestMockFetchContext* create() { | 164 static ImageResourceTestMockFetchContext* create() { |
| 171 return new ImageResourceTestMockFetchContext; | 165 return new ImageResourceTestMockFetchContext; |
| 172 } | 166 } |
| 173 | 167 |
| 174 virtual ~ImageResourceTestMockFetchContext() {} | 168 virtual ~ImageResourceTestMockFetchContext() {} |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 203 } |
| 210 | 204 |
| 211 ~ScopedRegisteredURL() { | 205 ~ScopedRegisteredURL() { |
| 212 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url); | 206 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url); |
| 213 } | 207 } |
| 214 | 208 |
| 215 private: | 209 private: |
| 216 KURL m_url; | 210 KURL m_url; |
| 217 }; | 211 }; |
| 218 | 212 |
| 219 } // namespace | 213 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { |
| 214 return AtomicString(String("bytes 0-" + String::number(rangeLength) + "/" + |
| 215 String::number(totalLength))); |
| 216 } |
| 220 | 217 |
| 221 TEST(ImageResourceTest, MultipartImage) { | 218 TEST(ImageResourceTest, MultipartImage) { |
| 222 ResourceFetcher* fetcher = | 219 ResourceFetcher* fetcher = |
| 223 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 220 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 221 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 225 ScopedRegisteredURL scopedRegisteredURL(testURL); | 222 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 226 | 223 |
| 227 // Emulate starting a real load, but don't expect any "real" | 224 // Emulate starting a real load, but don't expect any "real" |
| 228 // WebURLLoaderClient callbacks. | 225 // WebURLLoaderClient callbacks. |
| 229 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); | 226 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 | 332 |
| 336 Persistent<MockImageResourceClient> client = | 333 Persistent<MockImageResourceClient> client = |
| 337 new MockImageResourceClient(cachedImage); | 334 new MockImageResourceClient(cachedImage); |
| 338 | 335 |
| 339 // Send the image response. | 336 // Send the image response. |
| 340 cachedImage->responseReceived( | 337 cachedImage->responseReceived( |
| 341 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, | 338 ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, nullAtom, |
| 342 String()), | 339 String()), |
| 343 nullptr); | 340 nullptr); |
| 344 | 341 |
| 345 Vector<unsigned char> jpeg = jpegImage(); | |
| 346 cachedImage->responseReceived( | 342 cachedImage->responseReceived( |
| 347 ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), | 343 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, |
| 344 String()), |
| 348 nullptr); | 345 nullptr); |
| 349 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 346 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 350 jpeg.size()); | 347 sizeof(kJpegImage)); |
| 351 EXPECT_NE(0u, cachedImage->encodedSizeMemoryUsageForTesting()); | 348 EXPECT_NE(0u, cachedImage->encodedSizeMemoryUsageForTesting()); |
| 352 cachedImage->finish(); | 349 cachedImage->finish(); |
| 353 EXPECT_EQ(0u, cachedImage->encodedSizeMemoryUsageForTesting()); | 350 EXPECT_EQ(0u, cachedImage->encodedSizeMemoryUsageForTesting()); |
| 354 EXPECT_FALSE(cachedImage->errorOccurred()); | 351 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 355 ASSERT_TRUE(cachedImage->hasImage()); | 352 ASSERT_TRUE(cachedImage->hasImage()); |
| 356 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 353 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 357 EXPECT_TRUE(client->notifyFinishedCalled()); | 354 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 358 | 355 |
| 359 // The prune comes when the ImageResource still has clients. The image should | 356 // The prune comes when the ImageResource still has clients. The image should |
| 360 // not be deleted. | 357 // not be deleted. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 374 } | 371 } |
| 375 | 372 |
| 376 TEST(ImageResourceTest, UpdateBitmapImages) { | 373 TEST(ImageResourceTest, UpdateBitmapImages) { |
| 377 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); | 374 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); |
| 378 cachedImage->setStatus(Resource::Pending); | 375 cachedImage->setStatus(Resource::Pending); |
| 379 | 376 |
| 380 Persistent<MockImageResourceClient> client = | 377 Persistent<MockImageResourceClient> client = |
| 381 new MockImageResourceClient(cachedImage); | 378 new MockImageResourceClient(cachedImage); |
| 382 | 379 |
| 383 // Send the image response. | 380 // Send the image response. |
| 384 Vector<unsigned char> jpeg = jpegImage(); | |
| 385 cachedImage->responseReceived( | 381 cachedImage->responseReceived( |
| 386 ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), | 382 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, |
| 383 String()), |
| 387 nullptr); | 384 nullptr); |
| 388 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 385 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 389 jpeg.size()); | 386 sizeof(kJpegImage)); |
| 390 cachedImage->finish(); | 387 cachedImage->finish(); |
| 391 EXPECT_FALSE(cachedImage->errorOccurred()); | 388 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 392 ASSERT_TRUE(cachedImage->hasImage()); | 389 ASSERT_TRUE(cachedImage->hasImage()); |
| 393 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 390 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 394 EXPECT_EQ(2, client->imageChangedCount()); | 391 EXPECT_EQ(2, client->imageChangedCount()); |
| 395 EXPECT_TRUE(client->notifyFinishedCalled()); | 392 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 396 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 393 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 397 } | 394 } |
| 398 | 395 |
| 399 TEST(ImageResourceTest, ReloadIfLoFiAfterFinished) { | 396 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { |
| 400 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 397 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 401 ScopedRegisteredURL scopedRegisteredURL(testURL); | 398 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 402 ResourceRequest request = ResourceRequest(testURL); | 399 ResourceRequest request = ResourceRequest(testURL); |
| 403 request.setLoFiState(WebURLRequest::LoFiOn); | 400 request.setLoFiState(WebURLRequest::LoFiOn); |
| 404 ImageResource* cachedImage = ImageResource::create(request); | 401 ImageResource* cachedImage = ImageResource::create(request); |
| 405 cachedImage->setStatus(Resource::Pending); | 402 cachedImage->setStatus(Resource::Pending); |
| 406 | 403 |
| 407 Persistent<MockImageResourceClient> client = | 404 Persistent<MockImageResourceClient> client = |
| 408 new MockImageResourceClient(cachedImage); | 405 new MockImageResourceClient(cachedImage); |
| 409 ResourceFetcher* fetcher = | 406 ResourceFetcher* fetcher = |
| 410 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 407 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 411 | 408 |
| 412 // Send the image response. | 409 // Send the image response. |
| 413 Vector<unsigned char> jpeg = jpegImage(); | 410 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 414 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, | 411 nullAtom, String()); |
| 415 String()); | |
| 416 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 412 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 417 | 413 |
| 418 cachedImage->responseReceived(resourceResponse, nullptr); | 414 cachedImage->responseReceived(resourceResponse, nullptr); |
| 419 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 415 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 420 jpeg.size()); | 416 sizeof(kJpegImage)); |
| 421 cachedImage->finish(); | 417 cachedImage->finish(); |
| 422 EXPECT_FALSE(cachedImage->errorOccurred()); | 418 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 423 ASSERT_TRUE(cachedImage->hasImage()); | 419 ASSERT_TRUE(cachedImage->hasImage()); |
| 424 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 420 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 425 EXPECT_EQ(2, client->imageChangedCount()); | 421 EXPECT_EQ(2, client->imageChangedCount()); |
| 426 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged()); | 422 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 427 // The client should have been notified that the image load completed. | 423 // The client should have been notified that the image load completed. |
| 428 EXPECT_TRUE(client->notifyFinishedCalled()); | 424 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 429 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished()); | 425 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 430 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished()); | 426 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 431 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 427 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 432 EXPECT_EQ(1, cachedImage->getImage()->width()); | 428 EXPECT_EQ(1, cachedImage->getImage()->width()); |
| 433 EXPECT_EQ(1, cachedImage->getImage()->height()); | 429 EXPECT_EQ(1, cachedImage->getImage()->height()); |
| 434 | 430 |
| 435 // Call reloadIfLoFi() after the image has finished loading. | 431 // Call reloadIfLoFiOrPlaceholder() after the image has finished loading. |
| 436 cachedImage->reloadIfLoFi(fetcher); | 432 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); |
| 437 EXPECT_FALSE(cachedImage->errorOccurred()); | 433 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 438 EXPECT_FALSE(cachedImage->resourceBuffer()); | 434 EXPECT_FALSE(cachedImage->resourceBuffer()); |
| 439 EXPECT_FALSE(cachedImage->hasImage()); | 435 EXPECT_FALSE(cachedImage->hasImage()); |
| 440 EXPECT_EQ(3, client->imageChangedCount()); | 436 EXPECT_EQ(3, client->imageChangedCount()); |
| 441 | 437 |
| 442 Vector<unsigned char> jpeg2 = jpegImage2(); | |
| 443 cachedImage->loader()->didReceiveResponse( | 438 cachedImage->loader()->didReceiveResponse( |
| 444 nullptr, WrappedResourceResponse(resourceResponse), nullptr); | 439 nullptr, WrappedResourceResponse(resourceResponse), nullptr); |
| 445 cachedImage->loader()->didReceiveData( | 440 cachedImage->loader()->didReceiveData( |
| 446 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(), | 441 nullptr, reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2), |
| 447 jpeg2.size(), jpeg2.size()); | 442 sizeof(kJpegImage2), sizeof(kJpegImage2)); |
| 448 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size()); | 443 cachedImage->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage2)); |
| 449 EXPECT_FALSE(cachedImage->errorOccurred()); | 444 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 450 ASSERT_TRUE(cachedImage->hasImage()); | 445 ASSERT_TRUE(cachedImage->hasImage()); |
| 451 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 446 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 452 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged()); | 447 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
| 453 EXPECT_TRUE(client->notifyFinishedCalled()); | 448 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 454 // The client should not have been notified of completion again. | 449 // The client should not have been notified of completion again. |
| 455 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished()); | 450 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 456 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished()); | 451 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 457 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 452 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 458 EXPECT_EQ(50, cachedImage->getImage()->width()); | 453 EXPECT_EQ(50, cachedImage->getImage()->width()); |
| 459 EXPECT_EQ(50, cachedImage->getImage()->height()); | 454 EXPECT_EQ(50, cachedImage->getImage()->height()); |
| 460 } | 455 } |
| 461 | 456 |
| 462 TEST(ImageResourceTest, ReloadIfLoFiDuringFetch) { | 457 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
| 463 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 458 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 464 ScopedRegisteredURL scopedRegisteredURL(testURL); | 459 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 465 | 460 |
| 466 ResourceRequest request(testURL); | 461 ResourceRequest request(testURL); |
| 467 request.setLoFiState(WebURLRequest::LoFiOn); | 462 request.setLoFiState(WebURLRequest::LoFiOn); |
| 468 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | 463 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
| 469 ResourceFetcher* fetcher = | 464 ResourceFetcher* fetcher = |
| 470 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 465 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 471 | 466 |
| 472 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); | 467 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); |
| 473 Persistent<MockImageResourceClient> client = | 468 Persistent<MockImageResourceClient> client = |
| 474 new MockImageResourceClient(cachedImage); | 469 new MockImageResourceClient(cachedImage); |
| 475 | 470 |
| 476 // Send the image response. | 471 // Send the image response. |
| 477 Vector<unsigned char> jpeg = jpegImage(); | 472 ResourceResponse initialResourceResponse( |
| 478 | 473 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); |
| 479 ResourceResponse initialResourceResponse(testURL, "image/jpeg", jpeg.size(), | |
| 480 nullAtom, String()); | |
| 481 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 474 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 482 | 475 |
| 483 cachedImage->loader()->didReceiveResponse( | 476 cachedImage->loader()->didReceiveResponse( |
| 484 nullptr, WrappedResourceResponse(initialResourceResponse)); | 477 nullptr, WrappedResourceResponse(initialResourceResponse)); |
| 485 cachedImage->loader()->didReceiveData( | 478 cachedImage->loader()->didReceiveData( |
| 486 nullptr, reinterpret_cast<const char*>(jpeg.data()), jpeg.size(), | 479 nullptr, reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage), |
| 487 jpeg.size(), jpeg.size()); | 480 sizeof(kJpegImage), sizeof(kJpegImage)); |
| 488 | 481 |
| 489 EXPECT_FALSE(cachedImage->errorOccurred()); | 482 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 490 ASSERT_TRUE(cachedImage->hasImage()); | 483 ASSERT_TRUE(cachedImage->hasImage()); |
| 491 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 484 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 492 EXPECT_EQ(1, client->imageChangedCount()); | 485 EXPECT_EQ(1, client->imageChangedCount()); |
| 493 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged()); | 486 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 494 EXPECT_FALSE(client->notifyFinishedCalled()); | 487 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 495 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 488 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 496 EXPECT_EQ(1, cachedImage->getImage()->width()); | 489 EXPECT_EQ(1, cachedImage->getImage()->width()); |
| 497 EXPECT_EQ(1, cachedImage->getImage()->height()); | 490 EXPECT_EQ(1, cachedImage->getImage()->height()); |
| 498 | 491 |
| 499 // Call reloadIfLoFi() while the image is still loading. | 492 // Call reloadIfLoFiOrPlaceholder() while the image is still loading. |
| 500 cachedImage->reloadIfLoFi(fetcher); | 493 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); |
| 501 EXPECT_FALSE(cachedImage->errorOccurred()); | 494 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 502 EXPECT_FALSE(cachedImage->resourceBuffer()); | 495 EXPECT_FALSE(cachedImage->resourceBuffer()); |
| 503 EXPECT_FALSE(cachedImage->hasImage()); | 496 EXPECT_FALSE(cachedImage->hasImage()); |
| 504 EXPECT_EQ(2, client->imageChangedCount()); | 497 EXPECT_EQ(2, client->imageChangedCount()); |
| 505 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); | 498 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); |
| 506 // The client should not have been notified of completion yet, since the image | 499 // The client should not have been notified of completion yet, since the image |
| 507 // is still loading. | 500 // is still loading. |
| 508 EXPECT_FALSE(client->notifyFinishedCalled()); | 501 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 509 | 502 |
| 510 Vector<unsigned char> jpeg2 = jpegImage2(); | |
| 511 cachedImage->loader()->didReceiveResponse( | 503 cachedImage->loader()->didReceiveResponse( |
| 512 nullptr, WrappedResourceResponse(ResourceResponse( | 504 nullptr, |
| 513 testURL, "image/jpeg", jpeg.size(), nullAtom, String())), | 505 WrappedResourceResponse(ResourceResponse( |
| 506 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), |
| 514 nullptr); | 507 nullptr); |
| 515 cachedImage->loader()->didReceiveData( | 508 cachedImage->loader()->didReceiveData( |
| 516 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(), | 509 nullptr, reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2), |
| 517 jpeg2.size(), jpeg2.size()); | 510 sizeof(kJpegImage2), sizeof(kJpegImage2)); |
| 518 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size()); | 511 cachedImage->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage2)); |
| 519 | 512 |
| 520 EXPECT_FALSE(cachedImage->errorOccurred()); | 513 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 521 ASSERT_TRUE(cachedImage->hasImage()); | 514 ASSERT_TRUE(cachedImage->hasImage()); |
| 522 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 515 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 523 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged()); | 516 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
| 524 // The client should have been notified of completion only after the reload | 517 // The client should have been notified of completion only after the reload |
| 525 // completed. | 518 // completed. |
| 526 EXPECT_TRUE(client->notifyFinishedCalled()); | 519 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 527 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnNotifyFinished()); | 520 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished()); |
| 528 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnImageNotifyFinished()); | 521 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); |
| 529 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 522 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 530 EXPECT_EQ(50, cachedImage->getImage()->width()); | 523 EXPECT_EQ(50, cachedImage->getImage()->width()); |
| 531 EXPECT_EQ(50, cachedImage->getImage()->height()); | 524 EXPECT_EQ(50, cachedImage->getImage()->height()); |
| 532 } | 525 } |
| 533 | 526 |
| 527 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
| 528 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 529 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 530 |
| 531 ResourceFetcher* fetcher = |
| 532 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 533 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 534 request.setAllowImagePlaceholder(); |
| 535 ImageResource* image = ImageResource::fetch(request, fetcher); |
| 536 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 537 request.placeholderImageRequestType()); |
| 538 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 539 Persistent<MockImageResourceClient> client = |
| 540 new MockImageResourceClient(image); |
| 541 |
| 542 ResourceResponse response(testURL, "image/jpeg", |
| 543 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 544 String()); |
| 545 response.setHTTPStatusCode(206); |
| 546 response.setHTTPHeaderField( |
| 547 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 548 sizeof(kJpegImage))); |
| 549 image->loader()->didReceiveResponse(nullptr, |
| 550 WrappedResourceResponse(response)); |
| 551 image->loader()->didReceiveData(nullptr, |
| 552 reinterpret_cast<const char*>(kJpegImage), |
| 553 kJpegImageSubrangeWithDimensionsLength, |
| 554 kJpegImageSubrangeWithDimensionsLength, |
| 555 kJpegImageSubrangeWithDimensionsLength); |
| 556 image->loader()->didFinishLoading(nullptr, 0.0, |
| 557 kJpegImageSubrangeWithDimensionsLength); |
| 558 |
| 559 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 560 EXPECT_TRUE(image->isPlaceholder()); |
| 561 |
| 562 image->reloadIfLoFiOrPlaceholder(fetcher); |
| 563 |
| 564 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 565 EXPECT_FALSE(image->isPlaceholder()); |
| 566 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 567 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), |
| 568 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 569 |
| 570 image->loader()->cancel(); |
| 571 } |
| 572 |
| 534 TEST(ImageResourceTest, SVGImage) { | 573 TEST(ImageResourceTest, SVGImage) { |
| 535 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 574 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 536 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 575 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 537 Persistent<MockImageResourceClient> client = | 576 Persistent<MockImageResourceClient> client = |
| 538 new MockImageResourceClient(imageResource); | 577 new MockImageResourceClient(imageResource); |
| 539 | 578 |
| 540 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 579 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 580 strlen(kSvgImage)); |
| 541 | 581 |
| 542 EXPECT_FALSE(imageResource->errorOccurred()); | 582 EXPECT_FALSE(imageResource->errorOccurred()); |
| 543 ASSERT_TRUE(imageResource->hasImage()); | 583 ASSERT_TRUE(imageResource->hasImage()); |
| 544 EXPECT_FALSE(imageResource->getImage()->isNull()); | 584 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 545 EXPECT_EQ(1, client->imageChangedCount()); | 585 EXPECT_EQ(1, client->imageChangedCount()); |
| 546 EXPECT_TRUE(client->notifyFinishedCalled()); | 586 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 547 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 587 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 548 } | 588 } |
| 549 | 589 |
| 550 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { | 590 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { |
| 551 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 591 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 552 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 592 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 553 Persistent<MockImageResourceClient> client = | 593 Persistent<MockImageResourceClient> client = |
| 554 new MockImageResourceClient(imageResource); | 594 new MockImageResourceClient(imageResource); |
| 555 | 595 |
| 556 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 596 receiveResponse(imageResource, url, "image/jpeg", |
| 597 reinterpret_cast<const char*>(kJpegImage), |
| 598 sizeof(kJpegImage)); |
| 557 | 599 |
| 558 EXPECT_FALSE(imageResource->errorOccurred()); | 600 EXPECT_FALSE(imageResource->errorOccurred()); |
| 559 ASSERT_TRUE(imageResource->hasImage()); | 601 ASSERT_TRUE(imageResource->hasImage()); |
| 560 EXPECT_FALSE(imageResource->getImage()->isNull()); | 602 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 561 EXPECT_EQ(2, client->imageChangedCount()); | 603 EXPECT_EQ(2, client->imageChangedCount()); |
| 562 EXPECT_TRUE(client->notifyFinishedCalled()); | 604 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 563 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 605 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
| 564 EXPECT_EQ(1, imageResource->getImage()->width()); | 606 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 565 EXPECT_EQ(1, imageResource->getImage()->height()); | 607 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 566 | 608 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 580 EXPECT_EQ(1, imageResource->getImage()->width()); | 622 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 581 EXPECT_EQ(1, imageResource->getImage()->height()); | 623 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 582 } | 624 } |
| 583 | 625 |
| 584 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { | 626 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { |
| 585 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 627 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 586 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 628 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 587 Persistent<MockImageResourceClient> client = | 629 Persistent<MockImageResourceClient> client = |
| 588 new MockImageResourceClient(imageResource); | 630 new MockImageResourceClient(imageResource); |
| 589 | 631 |
| 590 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 632 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 633 strlen(kSvgImage)); |
| 591 | 634 |
| 592 EXPECT_FALSE(imageResource->errorOccurred()); | 635 EXPECT_FALSE(imageResource->errorOccurred()); |
| 593 ASSERT_TRUE(imageResource->hasImage()); | 636 ASSERT_TRUE(imageResource->hasImage()); |
| 594 EXPECT_FALSE(imageResource->getImage()->isNull()); | 637 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 595 EXPECT_EQ(1, client->imageChangedCount()); | 638 EXPECT_EQ(1, client->imageChangedCount()); |
| 596 EXPECT_TRUE(client->notifyFinishedCalled()); | 639 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 597 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 640 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 598 EXPECT_EQ(200, imageResource->getImage()->width()); | 641 EXPECT_EQ(200, imageResource->getImage()->width()); |
| 599 EXPECT_EQ(200, imageResource->getImage()->height()); | 642 EXPECT_EQ(200, imageResource->getImage()->height()); |
| 600 | 643 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 613 EXPECT_EQ(200, imageResource->getImage()->width()); | 656 EXPECT_EQ(200, imageResource->getImage()->width()); |
| 614 EXPECT_EQ(200, imageResource->getImage()->height()); | 657 EXPECT_EQ(200, imageResource->getImage()->height()); |
| 615 } | 658 } |
| 616 | 659 |
| 617 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { | 660 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { |
| 618 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 661 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 619 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 662 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 620 Persistent<MockImageResourceClient> client = | 663 Persistent<MockImageResourceClient> client = |
| 621 new MockImageResourceClient(imageResource); | 664 new MockImageResourceClient(imageResource); |
| 622 | 665 |
| 623 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 666 receiveResponse(imageResource, url, "image/jpeg", |
| 667 reinterpret_cast<const char*>(kJpegImage), |
| 668 sizeof(kJpegImage)); |
| 624 | 669 |
| 625 EXPECT_FALSE(imageResource->errorOccurred()); | 670 EXPECT_FALSE(imageResource->errorOccurred()); |
| 626 ASSERT_TRUE(imageResource->hasImage()); | 671 ASSERT_TRUE(imageResource->hasImage()); |
| 627 EXPECT_FALSE(imageResource->getImage()->isNull()); | 672 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 628 EXPECT_EQ(2, client->imageChangedCount()); | 673 EXPECT_EQ(2, client->imageChangedCount()); |
| 629 EXPECT_TRUE(client->notifyFinishedCalled()); | 674 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 630 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 675 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
| 631 EXPECT_EQ(1, imageResource->getImage()->width()); | 676 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 632 EXPECT_EQ(1, imageResource->getImage()->height()); | 677 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 633 | 678 |
| 634 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 679 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 635 receiveResponse(imageResource, url, "image/jpeg", jpegImage2()); | 680 receiveResponse(imageResource, url, "image/jpeg", |
| 681 reinterpret_cast<const char*>(kJpegImage2), |
| 682 sizeof(kJpegImage2)); |
| 636 | 683 |
| 637 EXPECT_FALSE(imageResource->errorOccurred()); | 684 EXPECT_FALSE(imageResource->errorOccurred()); |
| 638 ASSERT_TRUE(imageResource->hasImage()); | 685 ASSERT_TRUE(imageResource->hasImage()); |
| 639 EXPECT_FALSE(imageResource->getImage()->isNull()); | 686 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 640 EXPECT_EQ(4, client->imageChangedCount()); | 687 EXPECT_EQ(4, client->imageChangedCount()); |
| 641 EXPECT_TRUE(client->notifyFinishedCalled()); | 688 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 642 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 689 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
| 643 EXPECT_EQ(50, imageResource->getImage()->width()); | 690 EXPECT_EQ(50, imageResource->getImage()->width()); |
| 644 EXPECT_EQ(50, imageResource->getImage()->height()); | 691 EXPECT_EQ(50, imageResource->getImage()->height()); |
| 645 } | 692 } |
| 646 | 693 |
| 647 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { | 694 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { |
| 648 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 695 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 649 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 696 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 650 Persistent<MockImageResourceClient> client = | 697 Persistent<MockImageResourceClient> client = |
| 651 new MockImageResourceClient(imageResource); | 698 new MockImageResourceClient(imageResource); |
| 652 | 699 |
| 653 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 700 receiveResponse(imageResource, url, "image/jpeg", |
| 701 reinterpret_cast<const char*>(kJpegImage), |
| 702 sizeof(kJpegImage)); |
| 654 | 703 |
| 655 EXPECT_FALSE(imageResource->errorOccurred()); | 704 EXPECT_FALSE(imageResource->errorOccurred()); |
| 656 ASSERT_TRUE(imageResource->hasImage()); | 705 ASSERT_TRUE(imageResource->hasImage()); |
| 657 EXPECT_FALSE(imageResource->getImage()->isNull()); | 706 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 658 EXPECT_EQ(2, client->imageChangedCount()); | 707 EXPECT_EQ(2, client->imageChangedCount()); |
| 659 EXPECT_TRUE(client->notifyFinishedCalled()); | 708 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 660 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 709 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
| 661 EXPECT_EQ(1, imageResource->getImage()->width()); | 710 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 662 EXPECT_EQ(1, imageResource->getImage()->height()); | 711 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 663 | 712 |
| 664 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 713 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 665 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 714 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 715 strlen(kSvgImage)); |
| 666 | 716 |
| 667 EXPECT_FALSE(imageResource->errorOccurred()); | 717 EXPECT_FALSE(imageResource->errorOccurred()); |
| 668 ASSERT_TRUE(imageResource->hasImage()); | 718 ASSERT_TRUE(imageResource->hasImage()); |
| 669 EXPECT_FALSE(imageResource->getImage()->isNull()); | 719 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 670 EXPECT_EQ(3, client->imageChangedCount()); | 720 EXPECT_EQ(3, client->imageChangedCount()); |
| 671 EXPECT_TRUE(client->notifyFinishedCalled()); | 721 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 672 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 722 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 673 EXPECT_EQ(200, imageResource->getImage()->width()); | 723 EXPECT_EQ(200, imageResource->getImage()->width()); |
| 674 EXPECT_EQ(200, imageResource->getImage()->height()); | 724 EXPECT_EQ(200, imageResource->getImage()->height()); |
| 675 } | 725 } |
| 676 | 726 |
| 677 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { | 727 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { |
| 678 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 728 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 679 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 729 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 680 Persistent<MockImageResourceClient> client = | 730 Persistent<MockImageResourceClient> client = |
| 681 new MockImageResourceClient(imageResource); | 731 new MockImageResourceClient(imageResource); |
| 682 | 732 |
| 683 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 733 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 734 strlen(kSvgImage)); |
| 684 | 735 |
| 685 EXPECT_FALSE(imageResource->errorOccurred()); | 736 EXPECT_FALSE(imageResource->errorOccurred()); |
| 686 ASSERT_TRUE(imageResource->hasImage()); | 737 ASSERT_TRUE(imageResource->hasImage()); |
| 687 EXPECT_FALSE(imageResource->getImage()->isNull()); | 738 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 688 EXPECT_EQ(1, client->imageChangedCount()); | 739 EXPECT_EQ(1, client->imageChangedCount()); |
| 689 EXPECT_TRUE(client->notifyFinishedCalled()); | 740 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 690 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 741 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 691 EXPECT_EQ(200, imageResource->getImage()->width()); | 742 EXPECT_EQ(200, imageResource->getImage()->width()); |
| 692 EXPECT_EQ(200, imageResource->getImage()->height()); | 743 EXPECT_EQ(200, imageResource->getImage()->height()); |
| 693 | 744 |
| 694 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 745 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 695 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 746 receiveResponse(imageResource, url, "image/jpeg", |
| 747 reinterpret_cast<const char*>(kJpegImage), |
| 748 sizeof(kJpegImage)); |
| 696 | 749 |
| 697 EXPECT_FALSE(imageResource->errorOccurred()); | 750 EXPECT_FALSE(imageResource->errorOccurred()); |
| 698 ASSERT_TRUE(imageResource->hasImage()); | 751 ASSERT_TRUE(imageResource->hasImage()); |
| 699 EXPECT_FALSE(imageResource->getImage()->isNull()); | 752 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 700 EXPECT_EQ(3, client->imageChangedCount()); | 753 EXPECT_EQ(3, client->imageChangedCount()); |
| 701 EXPECT_TRUE(client->notifyFinishedCalled()); | 754 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 702 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 755 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
| 703 EXPECT_EQ(1, imageResource->getImage()->width()); | 756 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 704 EXPECT_EQ(1, imageResource->getImage()->height()); | 757 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 705 } | 758 } |
| 706 | 759 |
| 707 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { | 760 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { |
| 708 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 761 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 709 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 762 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 710 Persistent<MockImageResourceClient> client = | 763 Persistent<MockImageResourceClient> client = |
| 711 new MockImageResourceClient(imageResource); | 764 new MockImageResourceClient(imageResource); |
| 712 | 765 |
| 713 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 766 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 767 strlen(kSvgImage)); |
| 714 | 768 |
| 715 EXPECT_FALSE(imageResource->errorOccurred()); | 769 EXPECT_FALSE(imageResource->errorOccurred()); |
| 716 ASSERT_TRUE(imageResource->hasImage()); | 770 ASSERT_TRUE(imageResource->hasImage()); |
| 717 EXPECT_FALSE(imageResource->getImage()->isNull()); | 771 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 718 EXPECT_EQ(client->imageChangedCount(), 1); | 772 EXPECT_EQ(client->imageChangedCount(), 1); |
| 719 EXPECT_TRUE(client->notifyFinishedCalled()); | 773 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 720 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 774 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 721 EXPECT_EQ(200, imageResource->getImage()->width()); | 775 EXPECT_EQ(200, imageResource->getImage()->width()); |
| 722 EXPECT_EQ(200, imageResource->getImage()->height()); | 776 EXPECT_EQ(200, imageResource->getImage()->height()); |
| 723 | 777 |
| 724 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 778 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
| 725 receiveResponse(imageResource, url, "image/svg+xml", svgImage2()); | 779 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage2, |
| 780 strlen(kSvgImage2)); |
| 726 | 781 |
| 727 EXPECT_FALSE(imageResource->errorOccurred()); | 782 EXPECT_FALSE(imageResource->errorOccurred()); |
| 728 ASSERT_TRUE(imageResource->hasImage()); | 783 ASSERT_TRUE(imageResource->hasImage()); |
| 729 EXPECT_FALSE(imageResource->getImage()->isNull()); | 784 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 730 EXPECT_EQ(2, client->imageChangedCount()); | 785 EXPECT_EQ(2, client->imageChangedCount()); |
| 731 EXPECT_TRUE(client->notifyFinishedCalled()); | 786 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 732 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 787 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
| 733 EXPECT_EQ(300, imageResource->getImage()->width()); | 788 EXPECT_EQ(300, imageResource->getImage()->width()); |
| 734 EXPECT_EQ(300, imageResource->getImage()->height()); | 789 EXPECT_EQ(300, imageResource->getImage()->height()); |
| 735 } | 790 } |
| 736 | 791 |
| 737 // Tests for pruning. | 792 // Tests for pruning. |
| 738 | 793 |
| 739 TEST(ImageResourceTest, AddClientAfterPrune) { | 794 TEST(ImageResourceTest, AddClientAfterPrune) { |
| 740 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 795 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 741 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 796 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 742 | 797 |
| 743 // Adds a ResourceClient but not ImageResourceObserver. | 798 // Adds a ResourceClient but not ImageResourceObserver. |
| 744 Persistent<MockResourceClient> client1 = | 799 Persistent<MockResourceClient> client1 = |
| 745 new MockResourceClient(imageResource); | 800 new MockResourceClient(imageResource); |
| 746 | 801 |
| 747 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 802 receiveResponse(imageResource, url, "image/jpeg", |
| 803 reinterpret_cast<const char*>(kJpegImage), |
| 804 sizeof(kJpegImage)); |
| 748 | 805 |
| 749 EXPECT_FALSE(imageResource->errorOccurred()); | 806 EXPECT_FALSE(imageResource->errorOccurred()); |
| 750 ASSERT_TRUE(imageResource->hasImage()); | 807 ASSERT_TRUE(imageResource->hasImage()); |
| 751 EXPECT_FALSE(imageResource->getImage()->isNull()); | 808 EXPECT_FALSE(imageResource->getImage()->isNull()); |
| 752 EXPECT_EQ(1, imageResource->getImage()->width()); | 809 EXPECT_EQ(1, imageResource->getImage()->width()); |
| 753 EXPECT_EQ(1, imageResource->getImage()->height()); | 810 EXPECT_EQ(1, imageResource->getImage()->height()); |
| 754 EXPECT_TRUE(client1->notifyFinishedCalled()); | 811 EXPECT_TRUE(client1->notifyFinishedCalled()); |
| 755 | 812 |
| 756 client1->removeAsClient(); | 813 client1->removeAsClient(); |
| 757 | 814 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 784 cachedImage->loader()->didReceiveResponse( | 841 cachedImage->loader()->didReceiveResponse( |
| 785 nullptr, WrappedResourceResponse(ResourceResponse( | 842 nullptr, WrappedResourceResponse(ResourceResponse( |
| 786 testURL, "image/jpeg", 18, nullAtom, String())), | 843 testURL, "image/jpeg", 18, nullAtom, String())), |
| 787 nullptr); | 844 nullptr); |
| 788 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, | 845 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, |
| 789 18); | 846 18); |
| 790 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); | 847 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); |
| 791 EXPECT_FALSE(cachedImage->isLoading()); | 848 EXPECT_FALSE(cachedImage->isLoading()); |
| 792 } | 849 } |
| 793 | 850 |
| 851 TEST(ImageResourceTest, FetchDisallowPlaceholder) { |
| 852 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 853 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 854 |
| 855 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 856 ImageResource* image = ImageResource::fetch( |
| 857 request, |
| 858 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 859 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 860 request.placeholderImageRequestType()); |
| 861 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 862 EXPECT_FALSE(image->isPlaceholder()); |
| 863 Persistent<MockImageResourceClient> client = |
| 864 new MockImageResourceClient(image); |
| 865 |
| 866 image->loader()->didReceiveResponse( |
| 867 nullptr, |
| 868 WrappedResourceResponse(ResourceResponse( |
| 869 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 870 image->loader()->didReceiveData( |
| 871 nullptr, reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage), |
| 872 sizeof(kJpegImage), sizeof(kJpegImage)); |
| 873 image->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage)); |
| 874 |
| 875 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 876 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); |
| 877 EXPECT_FALSE(image->isPlaceholder()); |
| 878 EXPECT_LT(0, client->imageChangedCount()); |
| 879 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 880 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 881 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 882 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 883 |
| 884 ASSERT_TRUE(image->hasImage()); |
| 885 EXPECT_EQ(1, image->getImage()->width()); |
| 886 EXPECT_EQ(1, image->getImage()->height()); |
| 887 EXPECT_TRUE(image->getImage()->isBitmapImage()); |
| 888 } |
| 889 |
| 890 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) { |
| 891 KURL testURL(ParsedURLString, |
| 892 "data:image/jpeg;base64," + |
| 893 base64Encode(reinterpret_cast<const char*>(kJpegImage), |
| 894 sizeof(kJpegImage))); |
| 895 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 896 request.setAllowImagePlaceholder(); |
| 897 ImageResource* image = ImageResource::fetch( |
| 898 request, |
| 899 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 900 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 901 request.placeholderImageRequestType()); |
| 902 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 903 EXPECT_FALSE(image->isPlaceholder()); |
| 904 } |
| 905 |
| 906 TEST(ImageResourceTest, FetchAllowPlaceholderPostRequest) { |
| 907 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 908 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 909 ResourceRequest resourceRequest(testURL); |
| 910 resourceRequest.setHTTPMethod("POST"); |
| 911 FetchRequest request(resourceRequest, FetchInitiatorInfo()); |
| 912 request.setAllowImagePlaceholder(); |
| 913 ImageResource* image = ImageResource::fetch( |
| 914 request, |
| 915 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 916 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 917 request.placeholderImageRequestType()); |
| 918 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 919 EXPECT_FALSE(image->isPlaceholder()); |
| 920 |
| 921 image->loader()->cancel(); |
| 922 } |
| 923 |
| 924 TEST(ImageResourceTest, FetchAllowPlaceholderExistingRangeHeader) { |
| 925 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 926 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 927 ResourceRequest resourceRequest(testURL); |
| 928 resourceRequest.setHTTPHeaderField("range", "bytes=128-255"); |
| 929 FetchRequest request(resourceRequest, FetchInitiatorInfo()); |
| 930 request.setAllowImagePlaceholder(); |
| 931 ImageResource* image = ImageResource::fetch( |
| 932 request, |
| 933 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 934 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 935 request.placeholderImageRequestType()); |
| 936 EXPECT_EQ("bytes=128-255", image->resourceRequest().httpHeaderField("range")); |
| 937 EXPECT_FALSE(image->isPlaceholder()); |
| 938 |
| 939 image->loader()->cancel(); |
| 940 } |
| 941 |
| 942 TEST(ImageResourceTest, FetchAllowPlaceholderSuccessful) { |
| 943 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 944 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 945 |
| 946 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 947 request.setAllowImagePlaceholder(); |
| 948 ImageResource* image = ImageResource::fetch( |
| 949 request, |
| 950 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 951 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 952 request.placeholderImageRequestType()); |
| 953 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 954 EXPECT_TRUE(image->isPlaceholder()); |
| 955 Persistent<MockImageResourceClient> client = |
| 956 new MockImageResourceClient(image); |
| 957 |
| 958 ResourceResponse response(testURL, "image/jpeg", |
| 959 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 960 String()); |
| 961 response.setHTTPStatusCode(206); |
| 962 response.setHTTPHeaderField( |
| 963 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 964 sizeof(kJpegImage))); |
| 965 image->loader()->didReceiveResponse(nullptr, |
| 966 WrappedResourceResponse(response)); |
| 967 image->loader()->didReceiveData(nullptr, |
| 968 reinterpret_cast<const char*>(kJpegImage), |
| 969 kJpegImageSubrangeWithDimensionsLength, |
| 970 kJpegImageSubrangeWithDimensionsLength, |
| 971 kJpegImageSubrangeWithDimensionsLength); |
| 972 image->loader()->didFinishLoading(nullptr, 0.0, |
| 973 kJpegImageSubrangeWithDimensionsLength); |
| 974 |
| 975 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 976 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); |
| 977 EXPECT_TRUE(image->isPlaceholder()); |
| 978 EXPECT_LT(0, client->imageChangedCount()); |
| 979 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 980 client->encodedSizeOnLastImageChanged()); |
| 981 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 982 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 983 client->encodedSizeOnNotifyFinished()); |
| 984 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 985 client->encodedSizeOnImageNotifyFinished()); |
| 986 |
| 987 ASSERT_TRUE(image->hasImage()); |
| 988 EXPECT_EQ(1, image->getImage()->width()); |
| 989 EXPECT_EQ(1, image->getImage()->height()); |
| 990 EXPECT_FALSE(image->getImage()->isBitmapImage()); |
| 991 EXPECT_FALSE(image->getImage()->isSVGImage()); |
| 992 } |
| 993 |
| 994 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { |
| 995 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 996 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 997 |
| 998 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 999 request.setAllowImagePlaceholder(); |
| 1000 ImageResource* image = ImageResource::fetch( |
| 1001 request, |
| 1002 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 1003 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1004 request.placeholderImageRequestType()); |
| 1005 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 1006 EXPECT_TRUE(image->isPlaceholder()); |
| 1007 Persistent<MockImageResourceClient> client = |
| 1008 new MockImageResourceClient(image); |
| 1009 |
| 1010 const char kBadData[] = "notanimageresponse"; |
| 1011 |
| 1012 image->loader()->didReceiveResponse( |
| 1013 nullptr, |
| 1014 WrappedResourceResponse(ResourceResponse( |
| 1015 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); |
| 1016 image->loader()->didReceiveData(nullptr, kBadData, sizeof(kBadData), |
| 1017 sizeof(kBadData), sizeof(kBadData)); |
| 1018 |
| 1019 // The dimensions could not be extracted, so the full original image should be |
| 1020 // loading. |
| 1021 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1022 EXPECT_FALSE(image->isPlaceholder()); |
| 1023 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1024 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), |
| 1025 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1026 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 1027 |
| 1028 image->loader()->didReceiveResponse( |
| 1029 nullptr, |
| 1030 WrappedResourceResponse(ResourceResponse( |
| 1031 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 1032 image->loader()->didReceiveData( |
| 1033 nullptr, reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage), |
| 1034 sizeof(kJpegImage), sizeof(kJpegImage)); |
| 1035 image->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage)); |
| 1036 |
| 1037 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1038 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); |
| 1039 EXPECT_FALSE(image->isPlaceholder()); |
| 1040 EXPECT_LT(0, client->imageChangedCount()); |
| 1041 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 1042 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1043 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 1044 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 1045 |
| 1046 ASSERT_TRUE(image->hasImage()); |
| 1047 EXPECT_EQ(1, image->getImage()->width()); |
| 1048 EXPECT_EQ(1, image->getImage()->height()); |
| 1049 EXPECT_TRUE(image->getImage()->isBitmapImage()); |
| 1050 } |
| 1051 |
| 1052 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
| 1053 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1054 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1055 |
| 1056 ResourceFetcher* fetcher = |
| 1057 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1058 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1059 placeholderRequest.setAllowImagePlaceholder(); |
| 1060 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1061 Persistent<MockImageResourceClient> client = |
| 1062 new MockImageResourceClient(image); |
| 1063 |
| 1064 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1065 ImageResource* secondImage = |
| 1066 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1067 EXPECT_EQ(image, secondImage); |
| 1068 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1069 EXPECT_FALSE(image->isPlaceholder()); |
| 1070 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1071 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1072 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1073 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 1074 |
| 1075 image->loader()->cancel(); |
| 1076 } |
| 1077 |
| 1078 TEST(ImageResourceTest, |
| 1079 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { |
| 1080 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1081 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1082 |
| 1083 ResourceFetcher* fetcher = |
| 1084 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1085 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1086 placeholderRequest.setAllowImagePlaceholder(); |
| 1087 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1088 Persistent<MockImageResourceClient> client = |
| 1089 new MockImageResourceClient(image); |
| 1090 |
| 1091 ResourceResponse response(testURL, "image/jpeg", |
| 1092 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 1093 String()); |
| 1094 response.setHTTPStatusCode(206); |
| 1095 response.setHTTPHeaderField( |
| 1096 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 1097 sizeof(kJpegImage))); |
| 1098 image->loader()->didReceiveResponse(nullptr, |
| 1099 WrappedResourceResponse(response)); |
| 1100 image->loader()->didReceiveData(nullptr, |
| 1101 reinterpret_cast<const char*>(kJpegImage), |
| 1102 kJpegImageSubrangeWithDimensionsLength, |
| 1103 kJpegImageSubrangeWithDimensionsLength, |
| 1104 kJpegImageSubrangeWithDimensionsLength); |
| 1105 image->loader()->didFinishLoading(nullptr, 0.0, |
| 1106 kJpegImageSubrangeWithDimensionsLength); |
| 1107 |
| 1108 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1109 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); |
| 1110 EXPECT_TRUE(image->isPlaceholder()); |
| 1111 EXPECT_LT(0, client->imageChangedCount()); |
| 1112 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1113 |
| 1114 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1115 ImageResource* secondImage = |
| 1116 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1117 EXPECT_EQ(image, secondImage); |
| 1118 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1119 EXPECT_FALSE(image->isPlaceholder()); |
| 1120 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1121 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1122 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1123 |
| 1124 image->loader()->cancel(); |
| 1125 } |
| 1126 |
| 1127 } // namespace |
| 1128 |
| 794 } // namespace blink | 1129 } // namespace blink |
| OLD | NEW |