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 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 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(kJpegImage, sizeof(kJpegImage)); |
350 jpeg.size()); | |
351 cachedImage->finish(); | 347 cachedImage->finish(); |
352 EXPECT_FALSE(cachedImage->errorOccurred()); | 348 EXPECT_FALSE(cachedImage->errorOccurred()); |
353 ASSERT_TRUE(cachedImage->hasImage()); | 349 ASSERT_TRUE(cachedImage->hasImage()); |
354 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 350 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
355 EXPECT_TRUE(client->notifyFinishedCalled()); | 351 EXPECT_TRUE(client->notifyFinishedCalled()); |
356 | 352 |
357 // The prune comes when the ImageResource still has clients. The image should | 353 // The prune comes when the ImageResource still has clients. The image should |
358 // not be deleted. | 354 // not be deleted. |
359 cachedImage->prune(); | 355 cachedImage->prune(); |
360 EXPECT_TRUE(cachedImage->isAlive()); | 356 EXPECT_TRUE(cachedImage->isAlive()); |
(...skipping 11 matching lines...) Expand all Loading... |
372 } | 368 } |
373 | 369 |
374 TEST(ImageResourceTest, UpdateBitmapImages) { | 370 TEST(ImageResourceTest, UpdateBitmapImages) { |
375 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); | 371 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); |
376 cachedImage->setStatus(Resource::Pending); | 372 cachedImage->setStatus(Resource::Pending); |
377 | 373 |
378 Persistent<MockImageResourceClient> client = | 374 Persistent<MockImageResourceClient> client = |
379 new MockImageResourceClient(cachedImage); | 375 new MockImageResourceClient(cachedImage); |
380 | 376 |
381 // Send the image response. | 377 // Send the image response. |
382 Vector<unsigned char> jpeg = jpegImage(); | |
383 cachedImage->responseReceived( | 378 cachedImage->responseReceived( |
384 ResourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, String()), | 379 ResourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), nullAtom, |
| 380 String()), |
385 nullptr); | 381 nullptr); |
386 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 382 cachedImage->appendData(kJpegImage, sizeof(kJpegImage)); |
387 jpeg.size()); | |
388 cachedImage->finish(); | 383 cachedImage->finish(); |
389 EXPECT_FALSE(cachedImage->errorOccurred()); | 384 EXPECT_FALSE(cachedImage->errorOccurred()); |
390 ASSERT_TRUE(cachedImage->hasImage()); | 385 ASSERT_TRUE(cachedImage->hasImage()); |
391 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 386 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
392 EXPECT_EQ(2, client->imageChangedCount()); | 387 EXPECT_EQ(2, client->imageChangedCount()); |
393 EXPECT_TRUE(client->notifyFinishedCalled()); | 388 EXPECT_TRUE(client->notifyFinishedCalled()); |
394 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 389 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
395 } | 390 } |
396 | 391 |
397 TEST(ImageResourceTest, ReloadIfLoFiAfterFinished) { | 392 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { |
398 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 393 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
399 ScopedRegisteredURL scopedRegisteredURL(testURL); | 394 ScopedRegisteredURL scopedRegisteredURL(testURL); |
400 ResourceRequest request = ResourceRequest(testURL); | 395 ResourceRequest request = ResourceRequest(testURL); |
401 request.setLoFiState(WebURLRequest::LoFiOn); | 396 request.setLoFiState(WebURLRequest::LoFiOn); |
402 ImageResource* cachedImage = ImageResource::create(request); | 397 ImageResource* cachedImage = ImageResource::create(request); |
403 cachedImage->setStatus(Resource::Pending); | 398 cachedImage->setStatus(Resource::Pending); |
404 | 399 |
405 Persistent<MockImageResourceClient> client = | 400 Persistent<MockImageResourceClient> client = |
406 new MockImageResourceClient(cachedImage); | 401 new MockImageResourceClient(cachedImage); |
407 ResourceFetcher* fetcher = | 402 ResourceFetcher* fetcher = |
408 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 403 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
409 | 404 |
410 // Send the image response. | 405 // Send the image response. |
411 Vector<unsigned char> jpeg = jpegImage(); | 406 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), |
412 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, | 407 nullAtom, String()); |
413 String()); | |
414 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 408 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
415 | 409 |
416 cachedImage->responseReceived(resourceResponse, nullptr); | 410 cachedImage->responseReceived(resourceResponse, nullptr); |
417 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), | 411 cachedImage->appendData(kJpegImage, sizeof(kJpegImage)); |
418 jpeg.size()); | |
419 cachedImage->finish(); | 412 cachedImage->finish(); |
420 EXPECT_FALSE(cachedImage->errorOccurred()); | 413 EXPECT_FALSE(cachedImage->errorOccurred()); |
421 ASSERT_TRUE(cachedImage->hasImage()); | 414 ASSERT_TRUE(cachedImage->hasImage()); |
422 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 415 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
423 EXPECT_EQ(2, client->imageChangedCount()); | 416 EXPECT_EQ(2, client->imageChangedCount()); |
424 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged()); | 417 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
425 // The client should have been notified that the image load completed. | 418 // The client should have been notified that the image load completed. |
426 EXPECT_TRUE(client->notifyFinishedCalled()); | 419 EXPECT_TRUE(client->notifyFinishedCalled()); |
427 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished()); | 420 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
428 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished()); | 421 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
429 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 422 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
430 EXPECT_EQ(1, cachedImage->getImage()->width()); | 423 EXPECT_EQ(1, cachedImage->getImage()->width()); |
431 EXPECT_EQ(1, cachedImage->getImage()->height()); | 424 EXPECT_EQ(1, cachedImage->getImage()->height()); |
432 | 425 |
433 // Call reloadIfLoFi() after the image has finished loading. | 426 // Call reloadIfLoFiOrPlaceholder() after the image has finished loading. |
434 cachedImage->reloadIfLoFi(fetcher); | 427 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); |
435 EXPECT_FALSE(cachedImage->errorOccurred()); | 428 EXPECT_FALSE(cachedImage->errorOccurred()); |
436 EXPECT_FALSE(cachedImage->resourceBuffer()); | 429 EXPECT_FALSE(cachedImage->resourceBuffer()); |
437 EXPECT_FALSE(cachedImage->hasImage()); | 430 EXPECT_FALSE(cachedImage->hasImage()); |
438 EXPECT_EQ(3, client->imageChangedCount()); | 431 EXPECT_EQ(3, client->imageChangedCount()); |
439 | 432 |
440 Vector<unsigned char> jpeg2 = jpegImage2(); | |
441 cachedImage->loader()->didReceiveResponse( | 433 cachedImage->loader()->didReceiveResponse( |
442 nullptr, WrappedResourceResponse(resourceResponse), nullptr); | 434 nullptr, WrappedResourceResponse(resourceResponse), nullptr); |
443 cachedImage->loader()->didReceiveData( | 435 cachedImage->loader()->didReceiveData( |
444 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(), | 436 nullptr, kJpegImage2, sizeof(kJpegImage2), sizeof(kJpegImage2), |
445 jpeg2.size(), jpeg2.size()); | 437 sizeof(kJpegImage2)); |
446 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size()); | 438 cachedImage->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage2)); |
447 EXPECT_FALSE(cachedImage->errorOccurred()); | 439 EXPECT_FALSE(cachedImage->errorOccurred()); |
448 ASSERT_TRUE(cachedImage->hasImage()); | 440 ASSERT_TRUE(cachedImage->hasImage()); |
449 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 441 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
450 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged()); | 442 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
451 EXPECT_TRUE(client->notifyFinishedCalled()); | 443 EXPECT_TRUE(client->notifyFinishedCalled()); |
452 // The client should not have been notified of completion again. | 444 // The client should not have been notified of completion again. |
453 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished()); | 445 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
454 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished()); | 446 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
455 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 447 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
456 EXPECT_EQ(50, cachedImage->getImage()->width()); | 448 EXPECT_EQ(50, cachedImage->getImage()->width()); |
457 EXPECT_EQ(50, cachedImage->getImage()->height()); | 449 EXPECT_EQ(50, cachedImage->getImage()->height()); |
458 } | 450 } |
459 | 451 |
460 TEST(ImageResourceTest, ReloadIfLoFiDuringFetch) { | 452 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
461 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 453 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
462 ScopedRegisteredURL scopedRegisteredURL(testURL); | 454 ScopedRegisteredURL scopedRegisteredURL(testURL); |
463 | 455 |
464 ResourceRequest request(testURL); | 456 ResourceRequest request(testURL); |
465 request.setLoFiState(WebURLRequest::LoFiOn); | 457 request.setLoFiState(WebURLRequest::LoFiOn); |
466 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | 458 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
467 ResourceFetcher* fetcher = | 459 ResourceFetcher* fetcher = |
468 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 460 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
469 | 461 |
470 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); | 462 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); |
471 Persistent<MockImageResourceClient> client = | 463 Persistent<MockImageResourceClient> client = |
472 new MockImageResourceClient(cachedImage); | 464 new MockImageResourceClient(cachedImage); |
473 | 465 |
474 // Send the image response. | 466 // Send the image response. |
475 Vector<unsigned char> jpeg = jpegImage(); | 467 ResourceResponse initialResourceResponse( |
476 | 468 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); |
477 ResourceResponse initialResourceResponse(testURL, "image/jpeg", jpeg.size(), | |
478 nullAtom, String()); | |
479 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 469 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
480 | 470 |
481 cachedImage->loader()->didReceiveResponse( | 471 cachedImage->loader()->didReceiveResponse( |
482 nullptr, WrappedResourceResponse(initialResourceResponse)); | 472 nullptr, WrappedResourceResponse(initialResourceResponse)); |
483 cachedImage->loader()->didReceiveData( | 473 cachedImage->loader()->didReceiveData(nullptr, kJpegImage, sizeof(kJpegImage), |
484 nullptr, reinterpret_cast<const char*>(jpeg.data()), jpeg.size(), | 474 sizeof(kJpegImage), sizeof(kJpegImage)); |
485 jpeg.size(), jpeg.size()); | |
486 | 475 |
487 EXPECT_FALSE(cachedImage->errorOccurred()); | 476 EXPECT_FALSE(cachedImage->errorOccurred()); |
488 ASSERT_TRUE(cachedImage->hasImage()); | 477 ASSERT_TRUE(cachedImage->hasImage()); |
489 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 478 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
490 EXPECT_EQ(1, client->imageChangedCount()); | 479 EXPECT_EQ(1, client->imageChangedCount()); |
491 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged()); | 480 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
492 EXPECT_FALSE(client->notifyFinishedCalled()); | 481 EXPECT_FALSE(client->notifyFinishedCalled()); |
493 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 482 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
494 EXPECT_EQ(1, cachedImage->getImage()->width()); | 483 EXPECT_EQ(1, cachedImage->getImage()->width()); |
495 EXPECT_EQ(1, cachedImage->getImage()->height()); | 484 EXPECT_EQ(1, cachedImage->getImage()->height()); |
496 | 485 |
497 // Call reloadIfLoFi() while the image is still loading. | 486 // Call reloadIfLoFiOrPlaceholder() while the image is still loading. |
498 cachedImage->reloadIfLoFi(fetcher); | 487 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); |
499 EXPECT_FALSE(cachedImage->errorOccurred()); | 488 EXPECT_FALSE(cachedImage->errorOccurred()); |
500 EXPECT_FALSE(cachedImage->resourceBuffer()); | 489 EXPECT_FALSE(cachedImage->resourceBuffer()); |
501 EXPECT_FALSE(cachedImage->hasImage()); | 490 EXPECT_FALSE(cachedImage->hasImage()); |
502 EXPECT_EQ(2, client->imageChangedCount()); | 491 EXPECT_EQ(2, client->imageChangedCount()); |
503 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); | 492 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); |
504 // The client should not have been notified of completion yet, since the image | 493 // The client should not have been notified of completion yet, since the image |
505 // is still loading. | 494 // is still loading. |
506 EXPECT_FALSE(client->notifyFinishedCalled()); | 495 EXPECT_FALSE(client->notifyFinishedCalled()); |
507 | 496 |
508 Vector<unsigned char> jpeg2 = jpegImage2(); | |
509 cachedImage->loader()->didReceiveResponse( | 497 cachedImage->loader()->didReceiveResponse( |
510 nullptr, WrappedResourceResponse(ResourceResponse( | 498 nullptr, |
511 testURL, "image/jpeg", jpeg.size(), nullAtom, String())), | 499 WrappedResourceResponse(ResourceResponse( |
| 500 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), |
512 nullptr); | 501 nullptr); |
513 cachedImage->loader()->didReceiveData( | 502 cachedImage->loader()->didReceiveData( |
514 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(), | 503 nullptr, kJpegImage2, sizeof(kJpegImage2), sizeof(kJpegImage2), |
515 jpeg2.size(), jpeg2.size()); | 504 sizeof(kJpegImage2)); |
516 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size()); | 505 cachedImage->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage2)); |
517 | 506 |
518 EXPECT_FALSE(cachedImage->errorOccurred()); | 507 EXPECT_FALSE(cachedImage->errorOccurred()); |
519 ASSERT_TRUE(cachedImage->hasImage()); | 508 ASSERT_TRUE(cachedImage->hasImage()); |
520 EXPECT_FALSE(cachedImage->getImage()->isNull()); | 509 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
521 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged()); | 510 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
522 // The client should have been notified of completion only after the reload | 511 // The client should have been notified of completion only after the reload |
523 // completed. | 512 // completed. |
524 EXPECT_TRUE(client->notifyFinishedCalled()); | 513 EXPECT_TRUE(client->notifyFinishedCalled()); |
525 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnNotifyFinished()); | 514 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished()); |
526 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnImageNotifyFinished()); | 515 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); |
527 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); | 516 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
528 EXPECT_EQ(50, cachedImage->getImage()->width()); | 517 EXPECT_EQ(50, cachedImage->getImage()->width()); |
529 EXPECT_EQ(50, cachedImage->getImage()->height()); | 518 EXPECT_EQ(50, cachedImage->getImage()->height()); |
530 } | 519 } |
531 | 520 |
| 521 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
| 522 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 523 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 524 |
| 525 ResourceFetcher* fetcher = |
| 526 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 527 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 528 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 529 ImageResource* image = ImageResource::fetch(request, fetcher); |
| 530 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 531 request.placeholderImageRequestType()); |
| 532 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 533 Persistent<MockImageResourceClient> client = |
| 534 new MockImageResourceClient(image); |
| 535 |
| 536 ResourceResponse response(testURL, "image/jpeg", |
| 537 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 538 String()); |
| 539 response.setHTTPStatusCode(206); |
| 540 response.setHTTPHeaderField( |
| 541 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 542 sizeof(kJpegImage))); |
| 543 image->loader()->didReceiveResponse(nullptr, |
| 544 WrappedResourceResponse(response)); |
| 545 image->loader()->didReceiveData(nullptr, kJpegImage, |
| 546 kJpegImageSubrangeWithDimensionsLength, |
| 547 kJpegImageSubrangeWithDimensionsLength, |
| 548 kJpegImageSubrangeWithDimensionsLength); |
| 549 image->loader()->didFinishLoading(nullptr, 0.0, |
| 550 kJpegImageSubrangeWithDimensionsLength); |
| 551 |
| 552 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 553 EXPECT_TRUE(image->isPlaceholder()); |
| 554 |
| 555 image->reloadIfLoFiOrPlaceholder(fetcher); |
| 556 |
| 557 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 558 EXPECT_FALSE(image->isPlaceholder()); |
| 559 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 560 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), |
| 561 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 562 |
| 563 image->loader()->cancel(); |
| 564 } |
| 565 |
532 TEST(ImageResourceTest, SVGImage) { | 566 TEST(ImageResourceTest, SVGImage) { |
533 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 567 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
534 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 568 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
535 Persistent<MockImageResourceClient> client = | 569 Persistent<MockImageResourceClient> client = |
536 new MockImageResourceClient(imageResource); | 570 new MockImageResourceClient(imageResource); |
537 | 571 |
538 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 572 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 573 strlen(kSvgImage)); |
539 | 574 |
540 EXPECT_FALSE(imageResource->errorOccurred()); | 575 EXPECT_FALSE(imageResource->errorOccurred()); |
541 ASSERT_TRUE(imageResource->hasImage()); | 576 ASSERT_TRUE(imageResource->hasImage()); |
542 EXPECT_FALSE(imageResource->getImage()->isNull()); | 577 EXPECT_FALSE(imageResource->getImage()->isNull()); |
543 EXPECT_EQ(1, client->imageChangedCount()); | 578 EXPECT_EQ(1, client->imageChangedCount()); |
544 EXPECT_TRUE(client->notifyFinishedCalled()); | 579 EXPECT_TRUE(client->notifyFinishedCalled()); |
545 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 580 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
546 } | 581 } |
547 | 582 |
548 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { | 583 TEST(ImageResourceTest, SuccessfulRevalidationJpeg) { |
549 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 584 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
550 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 585 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
551 Persistent<MockImageResourceClient> client = | 586 Persistent<MockImageResourceClient> client = |
552 new MockImageResourceClient(imageResource); | 587 new MockImageResourceClient(imageResource); |
553 | 588 |
554 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 589 receiveResponse(imageResource, url, "image/jpeg", kJpegImage, |
| 590 sizeof(kJpegImage)); |
555 | 591 |
556 EXPECT_FALSE(imageResource->errorOccurred()); | 592 EXPECT_FALSE(imageResource->errorOccurred()); |
557 ASSERT_TRUE(imageResource->hasImage()); | 593 ASSERT_TRUE(imageResource->hasImage()); |
558 EXPECT_FALSE(imageResource->getImage()->isNull()); | 594 EXPECT_FALSE(imageResource->getImage()->isNull()); |
559 EXPECT_EQ(2, client->imageChangedCount()); | 595 EXPECT_EQ(2, client->imageChangedCount()); |
560 EXPECT_TRUE(client->notifyFinishedCalled()); | 596 EXPECT_TRUE(client->notifyFinishedCalled()); |
561 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 597 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
562 EXPECT_EQ(1, imageResource->getImage()->width()); | 598 EXPECT_EQ(1, imageResource->getImage()->width()); |
563 EXPECT_EQ(1, imageResource->getImage()->height()); | 599 EXPECT_EQ(1, imageResource->getImage()->height()); |
564 | 600 |
(...skipping 13 matching lines...) Expand all Loading... |
578 EXPECT_EQ(1, imageResource->getImage()->width()); | 614 EXPECT_EQ(1, imageResource->getImage()->width()); |
579 EXPECT_EQ(1, imageResource->getImage()->height()); | 615 EXPECT_EQ(1, imageResource->getImage()->height()); |
580 } | 616 } |
581 | 617 |
582 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { | 618 TEST(ImageResourceTest, SuccessfulRevalidationSvg) { |
583 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 619 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
584 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 620 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
585 Persistent<MockImageResourceClient> client = | 621 Persistent<MockImageResourceClient> client = |
586 new MockImageResourceClient(imageResource); | 622 new MockImageResourceClient(imageResource); |
587 | 623 |
588 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 624 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 625 strlen(kSvgImage)); |
589 | 626 |
590 EXPECT_FALSE(imageResource->errorOccurred()); | 627 EXPECT_FALSE(imageResource->errorOccurred()); |
591 ASSERT_TRUE(imageResource->hasImage()); | 628 ASSERT_TRUE(imageResource->hasImage()); |
592 EXPECT_FALSE(imageResource->getImage()->isNull()); | 629 EXPECT_FALSE(imageResource->getImage()->isNull()); |
593 EXPECT_EQ(1, client->imageChangedCount()); | 630 EXPECT_EQ(1, client->imageChangedCount()); |
594 EXPECT_TRUE(client->notifyFinishedCalled()); | 631 EXPECT_TRUE(client->notifyFinishedCalled()); |
595 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 632 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
596 EXPECT_EQ(200, imageResource->getImage()->width()); | 633 EXPECT_EQ(200, imageResource->getImage()->width()); |
597 EXPECT_EQ(200, imageResource->getImage()->height()); | 634 EXPECT_EQ(200, imageResource->getImage()->height()); |
598 | 635 |
(...skipping 12 matching lines...) Expand all Loading... |
611 EXPECT_EQ(200, imageResource->getImage()->width()); | 648 EXPECT_EQ(200, imageResource->getImage()->width()); |
612 EXPECT_EQ(200, imageResource->getImage()->height()); | 649 EXPECT_EQ(200, imageResource->getImage()->height()); |
613 } | 650 } |
614 | 651 |
615 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { | 652 TEST(ImageResourceTest, FailedRevalidationJpegToJpeg) { |
616 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 653 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
617 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 654 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
618 Persistent<MockImageResourceClient> client = | 655 Persistent<MockImageResourceClient> client = |
619 new MockImageResourceClient(imageResource); | 656 new MockImageResourceClient(imageResource); |
620 | 657 |
621 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 658 receiveResponse(imageResource, url, "image/jpeg", kJpegImage, |
| 659 sizeof(kJpegImage)); |
622 | 660 |
623 EXPECT_FALSE(imageResource->errorOccurred()); | 661 EXPECT_FALSE(imageResource->errorOccurred()); |
624 ASSERT_TRUE(imageResource->hasImage()); | 662 ASSERT_TRUE(imageResource->hasImage()); |
625 EXPECT_FALSE(imageResource->getImage()->isNull()); | 663 EXPECT_FALSE(imageResource->getImage()->isNull()); |
626 EXPECT_EQ(2, client->imageChangedCount()); | 664 EXPECT_EQ(2, client->imageChangedCount()); |
627 EXPECT_TRUE(client->notifyFinishedCalled()); | 665 EXPECT_TRUE(client->notifyFinishedCalled()); |
628 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 666 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
629 EXPECT_EQ(1, imageResource->getImage()->width()); | 667 EXPECT_EQ(1, imageResource->getImage()->width()); |
630 EXPECT_EQ(1, imageResource->getImage()->height()); | 668 EXPECT_EQ(1, imageResource->getImage()->height()); |
631 | 669 |
632 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 670 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
633 receiveResponse(imageResource, url, "image/jpeg", jpegImage2()); | 671 receiveResponse(imageResource, url, "image/jpeg", kJpegImage2, |
| 672 sizeof(kJpegImage2)); |
634 | 673 |
635 EXPECT_FALSE(imageResource->errorOccurred()); | 674 EXPECT_FALSE(imageResource->errorOccurred()); |
636 ASSERT_TRUE(imageResource->hasImage()); | 675 ASSERT_TRUE(imageResource->hasImage()); |
637 EXPECT_FALSE(imageResource->getImage()->isNull()); | 676 EXPECT_FALSE(imageResource->getImage()->isNull()); |
638 EXPECT_EQ(4, client->imageChangedCount()); | 677 EXPECT_EQ(4, client->imageChangedCount()); |
639 EXPECT_TRUE(client->notifyFinishedCalled()); | 678 EXPECT_TRUE(client->notifyFinishedCalled()); |
640 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 679 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
641 EXPECT_EQ(50, imageResource->getImage()->width()); | 680 EXPECT_EQ(50, imageResource->getImage()->width()); |
642 EXPECT_EQ(50, imageResource->getImage()->height()); | 681 EXPECT_EQ(50, imageResource->getImage()->height()); |
643 } | 682 } |
644 | 683 |
645 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { | 684 TEST(ImageResourceTest, FailedRevalidationJpegToSvg) { |
646 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 685 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
647 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 686 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
648 Persistent<MockImageResourceClient> client = | 687 Persistent<MockImageResourceClient> client = |
649 new MockImageResourceClient(imageResource); | 688 new MockImageResourceClient(imageResource); |
650 | 689 |
651 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 690 receiveResponse(imageResource, url, "image/jpeg", kJpegImage, |
| 691 sizeof(kJpegImage)); |
652 | 692 |
653 EXPECT_FALSE(imageResource->errorOccurred()); | 693 EXPECT_FALSE(imageResource->errorOccurred()); |
654 ASSERT_TRUE(imageResource->hasImage()); | 694 ASSERT_TRUE(imageResource->hasImage()); |
655 EXPECT_FALSE(imageResource->getImage()->isNull()); | 695 EXPECT_FALSE(imageResource->getImage()->isNull()); |
656 EXPECT_EQ(2, client->imageChangedCount()); | 696 EXPECT_EQ(2, client->imageChangedCount()); |
657 EXPECT_TRUE(client->notifyFinishedCalled()); | 697 EXPECT_TRUE(client->notifyFinishedCalled()); |
658 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 698 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
659 EXPECT_EQ(1, imageResource->getImage()->width()); | 699 EXPECT_EQ(1, imageResource->getImage()->width()); |
660 EXPECT_EQ(1, imageResource->getImage()->height()); | 700 EXPECT_EQ(1, imageResource->getImage()->height()); |
661 | 701 |
662 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 702 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
663 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 703 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 704 strlen(kSvgImage)); |
664 | 705 |
665 EXPECT_FALSE(imageResource->errorOccurred()); | 706 EXPECT_FALSE(imageResource->errorOccurred()); |
666 ASSERT_TRUE(imageResource->hasImage()); | 707 ASSERT_TRUE(imageResource->hasImage()); |
667 EXPECT_FALSE(imageResource->getImage()->isNull()); | 708 EXPECT_FALSE(imageResource->getImage()->isNull()); |
668 EXPECT_EQ(3, client->imageChangedCount()); | 709 EXPECT_EQ(3, client->imageChangedCount()); |
669 EXPECT_TRUE(client->notifyFinishedCalled()); | 710 EXPECT_TRUE(client->notifyFinishedCalled()); |
670 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 711 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
671 EXPECT_EQ(200, imageResource->getImage()->width()); | 712 EXPECT_EQ(200, imageResource->getImage()->width()); |
672 EXPECT_EQ(200, imageResource->getImage()->height()); | 713 EXPECT_EQ(200, imageResource->getImage()->height()); |
673 } | 714 } |
674 | 715 |
675 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { | 716 TEST(ImageResourceTest, FailedRevalidationSvgToJpeg) { |
676 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 717 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
677 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 718 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
678 Persistent<MockImageResourceClient> client = | 719 Persistent<MockImageResourceClient> client = |
679 new MockImageResourceClient(imageResource); | 720 new MockImageResourceClient(imageResource); |
680 | 721 |
681 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 722 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 723 strlen(kSvgImage)); |
682 | 724 |
683 EXPECT_FALSE(imageResource->errorOccurred()); | 725 EXPECT_FALSE(imageResource->errorOccurred()); |
684 ASSERT_TRUE(imageResource->hasImage()); | 726 ASSERT_TRUE(imageResource->hasImage()); |
685 EXPECT_FALSE(imageResource->getImage()->isNull()); | 727 EXPECT_FALSE(imageResource->getImage()->isNull()); |
686 EXPECT_EQ(1, client->imageChangedCount()); | 728 EXPECT_EQ(1, client->imageChangedCount()); |
687 EXPECT_TRUE(client->notifyFinishedCalled()); | 729 EXPECT_TRUE(client->notifyFinishedCalled()); |
688 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 730 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
689 EXPECT_EQ(200, imageResource->getImage()->width()); | 731 EXPECT_EQ(200, imageResource->getImage()->width()); |
690 EXPECT_EQ(200, imageResource->getImage()->height()); | 732 EXPECT_EQ(200, imageResource->getImage()->height()); |
691 | 733 |
692 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 734 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
693 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 735 receiveResponse(imageResource, url, "image/jpeg", kJpegImage, |
| 736 sizeof(kJpegImage)); |
694 | 737 |
695 EXPECT_FALSE(imageResource->errorOccurred()); | 738 EXPECT_FALSE(imageResource->errorOccurred()); |
696 ASSERT_TRUE(imageResource->hasImage()); | 739 ASSERT_TRUE(imageResource->hasImage()); |
697 EXPECT_FALSE(imageResource->getImage()->isNull()); | 740 EXPECT_FALSE(imageResource->getImage()->isNull()); |
698 EXPECT_EQ(3, client->imageChangedCount()); | 741 EXPECT_EQ(3, client->imageChangedCount()); |
699 EXPECT_TRUE(client->notifyFinishedCalled()); | 742 EXPECT_TRUE(client->notifyFinishedCalled()); |
700 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); | 743 EXPECT_TRUE(imageResource->getImage()->isBitmapImage()); |
701 EXPECT_EQ(1, imageResource->getImage()->width()); | 744 EXPECT_EQ(1, imageResource->getImage()->width()); |
702 EXPECT_EQ(1, imageResource->getImage()->height()); | 745 EXPECT_EQ(1, imageResource->getImage()->height()); |
703 } | 746 } |
704 | 747 |
705 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { | 748 TEST(ImageResourceTest, FailedRevalidationSvgToSvg) { |
706 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 749 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
707 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 750 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
708 Persistent<MockImageResourceClient> client = | 751 Persistent<MockImageResourceClient> client = |
709 new MockImageResourceClient(imageResource); | 752 new MockImageResourceClient(imageResource); |
710 | 753 |
711 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); | 754 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 755 strlen(kSvgImage)); |
712 | 756 |
713 EXPECT_FALSE(imageResource->errorOccurred()); | 757 EXPECT_FALSE(imageResource->errorOccurred()); |
714 ASSERT_TRUE(imageResource->hasImage()); | 758 ASSERT_TRUE(imageResource->hasImage()); |
715 EXPECT_FALSE(imageResource->getImage()->isNull()); | 759 EXPECT_FALSE(imageResource->getImage()->isNull()); |
716 EXPECT_EQ(client->imageChangedCount(), 1); | 760 EXPECT_EQ(client->imageChangedCount(), 1); |
717 EXPECT_TRUE(client->notifyFinishedCalled()); | 761 EXPECT_TRUE(client->notifyFinishedCalled()); |
718 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 762 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
719 EXPECT_EQ(200, imageResource->getImage()->width()); | 763 EXPECT_EQ(200, imageResource->getImage()->width()); |
720 EXPECT_EQ(200, imageResource->getImage()->height()); | 764 EXPECT_EQ(200, imageResource->getImage()->height()); |
721 | 765 |
722 imageResource->setRevalidatingRequest(ResourceRequest(url)); | 766 imageResource->setRevalidatingRequest(ResourceRequest(url)); |
723 receiveResponse(imageResource, url, "image/svg+xml", svgImage2()); | 767 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage2, |
| 768 strlen(kSvgImage2)); |
724 | 769 |
725 EXPECT_FALSE(imageResource->errorOccurred()); | 770 EXPECT_FALSE(imageResource->errorOccurred()); |
726 ASSERT_TRUE(imageResource->hasImage()); | 771 ASSERT_TRUE(imageResource->hasImage()); |
727 EXPECT_FALSE(imageResource->getImage()->isNull()); | 772 EXPECT_FALSE(imageResource->getImage()->isNull()); |
728 EXPECT_EQ(2, client->imageChangedCount()); | 773 EXPECT_EQ(2, client->imageChangedCount()); |
729 EXPECT_TRUE(client->notifyFinishedCalled()); | 774 EXPECT_TRUE(client->notifyFinishedCalled()); |
730 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); | 775 EXPECT_FALSE(imageResource->getImage()->isBitmapImage()); |
731 EXPECT_EQ(300, imageResource->getImage()->width()); | 776 EXPECT_EQ(300, imageResource->getImage()->width()); |
732 EXPECT_EQ(300, imageResource->getImage()->height()); | 777 EXPECT_EQ(300, imageResource->getImage()->height()); |
733 } | 778 } |
734 | 779 |
735 // Tests for pruning. | 780 // Tests for pruning. |
736 | 781 |
737 TEST(ImageResourceTest, AddClientAfterPrune) { | 782 TEST(ImageResourceTest, AddClientAfterPrune) { |
738 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 783 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
739 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 784 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
740 | 785 |
741 // Adds a ResourceClient but not ImageResourceObserver. | 786 // Adds a ResourceClient but not ImageResourceObserver. |
742 Persistent<MockResourceClient> client1 = | 787 Persistent<MockResourceClient> client1 = |
743 new MockResourceClient(imageResource); | 788 new MockResourceClient(imageResource); |
744 | 789 |
745 receiveResponse(imageResource, url, "image/jpeg", jpegImage()); | 790 receiveResponse(imageResource, url, "image/jpeg", kJpegImage, |
| 791 sizeof(kJpegImage)); |
746 | 792 |
747 EXPECT_FALSE(imageResource->errorOccurred()); | 793 EXPECT_FALSE(imageResource->errorOccurred()); |
748 ASSERT_TRUE(imageResource->hasImage()); | 794 ASSERT_TRUE(imageResource->hasImage()); |
749 EXPECT_FALSE(imageResource->getImage()->isNull()); | 795 EXPECT_FALSE(imageResource->getImage()->isNull()); |
750 EXPECT_EQ(1, imageResource->getImage()->width()); | 796 EXPECT_EQ(1, imageResource->getImage()->width()); |
751 EXPECT_EQ(1, imageResource->getImage()->height()); | 797 EXPECT_EQ(1, imageResource->getImage()->height()); |
752 EXPECT_TRUE(client1->notifyFinishedCalled()); | 798 EXPECT_TRUE(client1->notifyFinishedCalled()); |
753 | 799 |
754 client1->removeAsClient(); | 800 client1->removeAsClient(); |
755 | 801 |
(...skipping 26 matching lines...) Expand all Loading... |
782 cachedImage->loader()->didReceiveResponse( | 828 cachedImage->loader()->didReceiveResponse( |
783 nullptr, WrappedResourceResponse(ResourceResponse( | 829 nullptr, WrappedResourceResponse(ResourceResponse( |
784 testURL, "image/jpeg", 18, nullAtom, String())), | 830 testURL, "image/jpeg", 18, nullAtom, String())), |
785 nullptr); | 831 nullptr); |
786 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, | 832 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, |
787 18); | 833 18); |
788 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); | 834 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); |
789 EXPECT_FALSE(cachedImage->isLoading()); | 835 EXPECT_FALSE(cachedImage->isLoading()); |
790 } | 836 } |
791 | 837 |
| 838 TEST(ImageResourceTest, FetchDisallowPlaceholder) { |
| 839 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 840 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 841 |
| 842 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 843 ImageResource* image = ImageResource::fetch( |
| 844 request, |
| 845 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 846 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 847 request.placeholderImageRequestType()); |
| 848 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 849 EXPECT_FALSE(image->isPlaceholder()); |
| 850 Persistent<MockImageResourceClient> client = |
| 851 new MockImageResourceClient(image); |
| 852 |
| 853 image->loader()->didReceiveResponse( |
| 854 nullptr, |
| 855 WrappedResourceResponse(ResourceResponse( |
| 856 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 857 image->loader()->didReceiveData(nullptr, kJpegImage, sizeof(kJpegImage), |
| 858 sizeof(kJpegImage), sizeof(kJpegImage)); |
| 859 image->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage)); |
| 860 |
| 861 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 862 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); |
| 863 EXPECT_FALSE(image->isPlaceholder()); |
| 864 EXPECT_LT(0, client->imageChangedCount()); |
| 865 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 866 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 867 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 868 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 869 |
| 870 ASSERT_TRUE(image->hasImage()); |
| 871 EXPECT_EQ(1, image->getImage()->width()); |
| 872 EXPECT_EQ(1, image->getImage()->height()); |
| 873 EXPECT_TRUE(image->getImage()->isBitmapImage()); |
| 874 } |
| 875 |
| 876 TEST(ImageResourceTest, FetchAllowPlaceholderDataURL) { |
| 877 KURL testURL( |
| 878 ParsedURLString, |
| 879 "data:image/jpeg;base64," + base64Encode(kJpegImage, sizeof(kJpegImage))); |
| 880 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 881 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 882 ImageResource* image = ImageResource::fetch( |
| 883 request, |
| 884 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 885 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 886 request.placeholderImageRequestType()); |
| 887 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 888 EXPECT_FALSE(image->isPlaceholder()); |
| 889 } |
| 890 |
| 891 TEST(ImageResourceTest, FetchAllowPlaceholderPostRequest) { |
| 892 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 893 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 894 ResourceRequest resourceRequest(testURL); |
| 895 resourceRequest.setHTTPMethod("POST"); |
| 896 FetchRequest request(resourceRequest, FetchInitiatorInfo()); |
| 897 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 898 ImageResource* image = ImageResource::fetch( |
| 899 request, |
| 900 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 901 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 902 request.placeholderImageRequestType()); |
| 903 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 904 EXPECT_FALSE(image->isPlaceholder()); |
| 905 |
| 906 image->loader()->cancel(); |
| 907 } |
| 908 |
| 909 TEST(ImageResourceTest, FetchAllowPlaceholderExistingRangeHeader) { |
| 910 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 911 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 912 ResourceRequest resourceRequest(testURL); |
| 913 resourceRequest.setHTTPHeaderField("range", "bytes=128-255"); |
| 914 FetchRequest request(resourceRequest, FetchInitiatorInfo()); |
| 915 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 916 ImageResource* image = ImageResource::fetch( |
| 917 request, |
| 918 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 919 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
| 920 request.placeholderImageRequestType()); |
| 921 EXPECT_EQ("bytes=128-255", image->resourceRequest().httpHeaderField("range")); |
| 922 EXPECT_FALSE(image->isPlaceholder()); |
| 923 |
| 924 image->loader()->cancel(); |
| 925 } |
| 926 |
| 927 TEST(ImageResourceTest, FetchAllowPlaceholderSuccessful) { |
| 928 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 929 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 930 |
| 931 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 932 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 933 ImageResource* image = ImageResource::fetch( |
| 934 request, |
| 935 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 936 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 937 request.placeholderImageRequestType()); |
| 938 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 939 EXPECT_TRUE(image->isPlaceholder()); |
| 940 Persistent<MockImageResourceClient> client = |
| 941 new MockImageResourceClient(image); |
| 942 |
| 943 ResourceResponse response(testURL, "image/jpeg", |
| 944 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 945 String()); |
| 946 response.setHTTPStatusCode(206); |
| 947 response.setHTTPHeaderField( |
| 948 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 949 sizeof(kJpegImage))); |
| 950 image->loader()->didReceiveResponse(nullptr, |
| 951 WrappedResourceResponse(response)); |
| 952 image->loader()->didReceiveData(nullptr, kJpegImage, |
| 953 kJpegImageSubrangeWithDimensionsLength, |
| 954 kJpegImageSubrangeWithDimensionsLength, |
| 955 kJpegImageSubrangeWithDimensionsLength); |
| 956 image->loader()->didFinishLoading(nullptr, 0.0, |
| 957 kJpegImageSubrangeWithDimensionsLength); |
| 958 |
| 959 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 960 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); |
| 961 EXPECT_TRUE(image->isPlaceholder()); |
| 962 EXPECT_LT(0, client->imageChangedCount()); |
| 963 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 964 client->encodedSizeOnLastImageChanged()); |
| 965 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 966 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 967 client->encodedSizeOnNotifyFinished()); |
| 968 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, |
| 969 client->encodedSizeOnImageNotifyFinished()); |
| 970 |
| 971 ASSERT_TRUE(image->hasImage()); |
| 972 EXPECT_EQ(1, image->getImage()->width()); |
| 973 EXPECT_EQ(1, image->getImage()->height()); |
| 974 EXPECT_FALSE(image->getImage()->isBitmapImage()); |
| 975 EXPECT_FALSE(image->getImage()->isSVGImage()); |
| 976 } |
| 977 |
| 978 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { |
| 979 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 980 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 981 |
| 982 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 983 request.setPlaceholderImageRequestType(FetchRequest::AllowPlaceholder); |
| 984 ImageResource* image = ImageResource::fetch( |
| 985 request, |
| 986 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 987 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 988 request.placeholderImageRequestType()); |
| 989 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 990 EXPECT_TRUE(image->isPlaceholder()); |
| 991 Persistent<MockImageResourceClient> client = |
| 992 new MockImageResourceClient(image); |
| 993 |
| 994 const char kBadData[] = "notanimageresponse"; |
| 995 |
| 996 image->loader()->didReceiveResponse( |
| 997 nullptr, |
| 998 WrappedResourceResponse(ResourceResponse( |
| 999 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); |
| 1000 image->loader()->didReceiveData(nullptr, kBadData, sizeof(kBadData), |
| 1001 sizeof(kBadData), sizeof(kBadData)); |
| 1002 |
| 1003 // The dimensions could not be extracted, so the full original image should be |
| 1004 // loading. |
| 1005 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1006 EXPECT_FALSE(image->isPlaceholder()); |
| 1007 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1008 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), |
| 1009 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1010 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 1011 |
| 1012 image->loader()->didReceiveResponse( |
| 1013 nullptr, |
| 1014 WrappedResourceResponse(ResourceResponse( |
| 1015 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 1016 image->loader()->didReceiveData(nullptr, kJpegImage, sizeof(kJpegImage), |
| 1017 sizeof(kJpegImage), sizeof(kJpegImage)); |
| 1018 image->loader()->didFinishLoading(nullptr, 0.0, sizeof(kJpegImage)); |
| 1019 |
| 1020 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1021 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); |
| 1022 EXPECT_FALSE(image->isPlaceholder()); |
| 1023 EXPECT_LT(0, client->imageChangedCount()); |
| 1024 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 1025 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1026 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 1027 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 1028 |
| 1029 ASSERT_TRUE(image->hasImage()); |
| 1030 EXPECT_EQ(1, image->getImage()->width()); |
| 1031 EXPECT_EQ(1, image->getImage()->height()); |
| 1032 EXPECT_TRUE(image->getImage()->isBitmapImage()); |
| 1033 } |
| 1034 |
| 1035 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
| 1036 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1037 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1038 |
| 1039 ResourceFetcher* fetcher = |
| 1040 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1041 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1042 placeholderRequest.setPlaceholderImageRequestType( |
| 1043 FetchRequest::AllowPlaceholder); |
| 1044 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1045 Persistent<MockImageResourceClient> client = |
| 1046 new MockImageResourceClient(image); |
| 1047 |
| 1048 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1049 nonPlaceholderRequest.setPlaceholderImageRequestType( |
| 1050 FetchRequest::DisallowPlaceholder); |
| 1051 ImageResource* secondImage = |
| 1052 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1053 EXPECT_EQ(image, secondImage); |
| 1054 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1055 EXPECT_FALSE(image->isPlaceholder()); |
| 1056 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1057 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1058 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1059 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 1060 |
| 1061 image->loader()->cancel(); |
| 1062 } |
| 1063 |
| 1064 TEST(ImageResourceTest, |
| 1065 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { |
| 1066 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1067 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1068 |
| 1069 ResourceFetcher* fetcher = |
| 1070 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1071 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1072 placeholderRequest.setPlaceholderImageRequestType( |
| 1073 FetchRequest::AllowPlaceholder); |
| 1074 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1075 Persistent<MockImageResourceClient> client = |
| 1076 new MockImageResourceClient(image); |
| 1077 |
| 1078 ResourceResponse response(testURL, "image/jpeg", |
| 1079 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 1080 String()); |
| 1081 response.setHTTPStatusCode(206); |
| 1082 response.setHTTPHeaderField( |
| 1083 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 1084 sizeof(kJpegImage))); |
| 1085 image->loader()->didReceiveResponse(nullptr, |
| 1086 WrappedResourceResponse(response)); |
| 1087 image->loader()->didReceiveData(nullptr, kJpegImage, |
| 1088 kJpegImageSubrangeWithDimensionsLength, |
| 1089 kJpegImageSubrangeWithDimensionsLength, |
| 1090 kJpegImageSubrangeWithDimensionsLength); |
| 1091 image->loader()->didFinishLoading(nullptr, 0.0, |
| 1092 kJpegImageSubrangeWithDimensionsLength); |
| 1093 |
| 1094 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1095 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); |
| 1096 EXPECT_TRUE(image->isPlaceholder()); |
| 1097 EXPECT_LT(0, client->imageChangedCount()); |
| 1098 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1099 |
| 1100 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1101 nonPlaceholderRequest.setPlaceholderImageRequestType( |
| 1102 FetchRequest::DisallowPlaceholder); |
| 1103 ImageResource* secondImage = |
| 1104 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1105 EXPECT_EQ(image, secondImage); |
| 1106 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1107 EXPECT_FALSE(image->isPlaceholder()); |
| 1108 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1109 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1110 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1111 |
| 1112 image->loader()->cancel(); |
| 1113 } |
| 1114 |
| 1115 } // namespace |
| 1116 |
792 } // namespace blink | 1117 } // namespace blink |
OLD | NEW |