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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "platform/SharedBuffer.h" | 44 #include "platform/SharedBuffer.h" |
45 #include "public/platform/Platform.h" | 45 #include "public/platform/Platform.h" |
46 #include "public/platform/WebURL.h" | 46 #include "public/platform/WebURL.h" |
47 #include "public/platform/WebURLResponse.h" | 47 #include "public/platform/WebURLResponse.h" |
48 #include "public/platform/WebUnitTestSupport.h" | 48 #include "public/platform/WebUnitTestSupport.h" |
49 | 49 |
50 using namespace blink; | 50 using namespace blink; |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
| 54 static Vector<unsigned char> jpegImage() |
| 55 { |
| 56 Vector<unsigned char> jpeg; |
| 57 |
| 58 static const unsigned char data[] = { |
| 59 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, |
| 60 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, |
| 61 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff,
0xdb, 0x00, 0x43, |
| 62 0x00, 0x05, 0x03, 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05,
0x05, 0x05, 0x06, |
| 63 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c,
0x11, 0x0f, 0x12, |
| 64 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, 0x13, 0x14, 0x1a,
0x15, 0x11, 0x11, |
| 65 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22,
0x24, 0x22, 0x1e, |
| 66 0x24, 0x1c, 0x1e, 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05,
0x05, 0x07, 0x06, |
| 67 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, |
| 68 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, |
| 69 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, |
| 70 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0xff, |
| 71 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, |
| 72 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, |
| 73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xff,
0xc4, 0x00, 0x14, |
| 74 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, |
| 75 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, |
| 76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4,
0x00, 0x14, 0x11, |
| 77 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, |
| 78 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
0x11, 0x00, 0x3f, |
| 79 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9 |
| 80 }; |
| 81 |
| 82 jpeg.append(data, sizeof(data)); |
| 83 return jpeg; |
| 84 } |
| 85 |
54 TEST(ImageResourceTest, MultipartImage) | 86 TEST(ImageResourceTest, MultipartImage) |
55 { | 87 { |
56 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); | 88 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); |
57 cachedImage->setLoading(true); | 89 cachedImage->setLoading(true); |
58 | 90 |
59 MockImageResourceClient client; | 91 MockImageResourceClient client; |
60 cachedImage->addClient(&client); | 92 cachedImage->addClient(&client); |
61 | 93 |
62 // Send the multipart response. No image or data buffer is created. | 94 // Send the multipart response. No image or data buffer is created. |
63 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String())); | 95 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String())); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) | 163 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) |
132 { | 164 { |
133 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); | 165 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); |
134 cachedImage->setLoading(true); | 166 cachedImage->setLoading(true); |
135 | 167 |
136 MockImageResourceClient client; | 168 MockImageResourceClient client; |
137 cachedImage->addClient(&client); | 169 cachedImage->addClient(&client); |
138 | 170 |
139 // Send the image response. | 171 // Send the image response. |
140 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String())); | 172 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String())); |
141 static const unsigned char jpegData[] = { | |
142 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, | |
143 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, | |
144 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff,
0xdb, 0x00, 0x43, | |
145 0x00, 0x05, 0x03, 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05,
0x05, 0x05, 0x06, | |
146 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c,
0x11, 0x0f, 0x12, | |
147 0x12, 0x11, 0x0f, 0x11, 0x11, 0x13, 0x16, 0x1c, 0x17, 0x13, 0x14, 0x1a,
0x15, 0x11, 0x11, | |
148 0x18, 0x21, 0x18, 0x1a, 0x1d, 0x1d, 0x1f, 0x1f, 0x1f, 0x13, 0x17, 0x22,
0x24, 0x22, 0x1e, | |
149 0x24, 0x1c, 0x1e, 0x1f, 0x1e, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x05, 0x05,
0x05, 0x07, 0x06, | |
150 0x07, 0x0e, 0x08, 0x08, 0x0e, 0x1e, 0x14, 0x11, 0x14, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
151 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
152 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, | |
153 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0xff, | |
154 0xc0, 0x00, 0x11, 0x08, 0x00, 0x01, 0x00, 0x01, 0x03, 0x01, 0x22, 0x00,
0x02, 0x11, 0x01, | |
155 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00, 0x15, 0x00, 0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, | |
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xff,
0xc4, 0x00, 0x14, | |
157 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
158 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4,
0x00, 0x14, 0x11, | |
160 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, | |
161 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
0x11, 0x00, 0x3f, | |
162 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9 | |
163 }; | |
164 | 173 |
165 unsigned jpegDataLength = sizeof(jpegData); | 174 Vector<unsigned char> jpeg = jpegImage(); |
166 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpegDat
aLength, nullAtom, String())); | 175 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String())); |
167 cachedImage->appendData(reinterpret_cast<const char*>(jpegData), jpegDataLen
gth); | 176 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); |
168 cachedImage->finish(); | 177 cachedImage->finish(); |
169 ASSERT_FALSE(cachedImage->errorOccurred()); | 178 ASSERT_FALSE(cachedImage->errorOccurred()); |
170 ASSERT_TRUE(cachedImage->hasImage()); | 179 ASSERT_TRUE(cachedImage->hasImage()); |
171 ASSERT_FALSE(cachedImage->image()->isNull()); | 180 ASSERT_FALSE(cachedImage->image()->isNull()); |
172 ASSERT_TRUE(client.notifyFinishedCalled()); | 181 ASSERT_TRUE(client.notifyFinishedCalled()); |
173 | 182 |
174 // The prune comes when the ImageResource still has clients. The image shoul
d not be deleted. | 183 // The prune comes when the ImageResource still has clients. The image shoul
d not be deleted. |
175 cachedImage->prune(); | 184 cachedImage->prune(); |
176 ASSERT_TRUE(cachedImage->hasClients()); | 185 ASSERT_TRUE(cachedImage->hasClients()); |
177 ASSERT_TRUE(cachedImage->hasImage()); | 186 ASSERT_TRUE(cachedImage->hasImage()); |
178 ASSERT_FALSE(cachedImage->image()->isNull()); | 187 ASSERT_FALSE(cachedImage->image()->isNull()); |
179 | 188 |
180 // The ImageResource no longer has clients. The image should be deleted by p
rune. | 189 // The ImageResource no longer has clients. The image should be deleted by p
rune. |
181 cachedImage->removeClient(&client); | 190 cachedImage->removeClient(&client); |
182 cachedImage->prune(); | 191 cachedImage->prune(); |
183 ASSERT_FALSE(cachedImage->hasClients()); | 192 ASSERT_FALSE(cachedImage->hasClients()); |
184 ASSERT_FALSE(cachedImage->hasImage()); | 193 ASSERT_FALSE(cachedImage->hasImage()); |
185 ASSERT_TRUE(cachedImage->image()->isNull()); | 194 ASSERT_TRUE(cachedImage->image()->isNull()); |
186 } | 195 } |
187 | 196 |
| 197 TEST(ImageResourceTest, UpdateBitmapImages) |
| 198 { |
| 199 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); |
| 200 cachedImage->setLoading(true); |
| 201 |
| 202 MockImageResourceClient client; |
| 203 cachedImage->addClient(&client); |
| 204 |
| 205 // Send the image response. |
| 206 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String())); |
| 207 |
| 208 Vector<unsigned char> jpeg = jpegImage(); |
| 209 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String())); |
| 210 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); |
| 211 cachedImage->finish(); |
| 212 ASSERT_FALSE(cachedImage->errorOccurred()); |
| 213 ASSERT_TRUE(cachedImage->hasImage()); |
| 214 ASSERT_FALSE(cachedImage->image()->isNull()); |
| 215 ASSERT_EQ(client.imageChangedCount(), 1); |
| 216 ASSERT_TRUE(client.notifyFinishedCalled()); |
| 217 |
| 218 HashSet<ImageResource*> bitmapImages; |
| 219 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); |
| 220 bitmapImages.add(cachedImage.get()); |
| 221 |
| 222 // Updating bitmap resources produces image changed callbacks on their clien
ts. |
| 223 ImageResource::updateBitmapImages(bitmapImages); |
| 224 ASSERT_EQ(client.imageChangedCount(), 2); |
| 225 ImageResource::updateBitmapImages(bitmapImages, true); |
| 226 ASSERT_EQ(client.imageChangedCount(), 3); |
| 227 } |
| 228 |
188 } // namespace | 229 } // namespace |
OLD | NEW |