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 30 matching lines...) Expand all Loading... |
41 #include "platform/SharedBuffer.h" | 41 #include "platform/SharedBuffer.h" |
42 #include "platform/exported/WrappedResourceResponse.h" | 42 #include "platform/exported/WrappedResourceResponse.h" |
43 #include "platform/graphics/Image.h" | 43 #include "platform/graphics/Image.h" |
44 #include "platform/testing/URLTestHelpers.h" | 44 #include "platform/testing/URLTestHelpers.h" |
45 #include "platform/testing/UnitTestHelpers.h" | 45 #include "platform/testing/UnitTestHelpers.h" |
46 #include "public/platform/Platform.h" | 46 #include "public/platform/Platform.h" |
47 #include "public/platform/WebURL.h" | 47 #include "public/platform/WebURL.h" |
48 #include "public/platform/WebURLResponse.h" | 48 #include "public/platform/WebURLResponse.h" |
49 #include "public/platform/WebUnitTestSupport.h" | 49 #include "public/platform/WebUnitTestSupport.h" |
50 | 50 |
| 51 #include <gtest/gtest.h> |
| 52 |
51 namespace blink { | 53 namespace blink { |
52 | 54 |
53 static Vector<unsigned char> jpegImage() | 55 static Vector<unsigned char> jpegImage() |
54 { | 56 { |
55 Vector<unsigned char> jpeg; | 57 Vector<unsigned char> jpeg; |
56 | 58 |
57 static const unsigned char data[] = { | 59 static const unsigned char data[] = { |
58 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, | 60 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, |
59 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, | 61 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, 0x43, 0x72, 0x65,
0x61, 0x74, 0x65, |
60 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff,
0xdb, 0x00, 0x43, | 62 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff,
0xdb, 0x00, 0x43, |
(...skipping 26 matching lines...) Expand all Loading... |
87 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); | 89 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); |
88 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 90 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
89 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); | 91 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); |
90 | 92 |
91 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli
ent callbacks. | 93 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli
ent callbacks. |
92 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); | 94 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); |
93 cachedImage->setIdentifier(createUniqueIdentifier()); | 95 cachedImage->setIdentifier(createUniqueIdentifier()); |
94 cachedImage->load(fetcher, ResourceLoaderOptions()); | 96 cachedImage->load(fetcher, ResourceLoaderOptions()); |
95 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | 97 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); |
96 | 98 |
97 MockImageResourceClient client; | 99 MockImageResourceClient client(cachedImage); |
98 cachedImage->addClient(&client); | |
99 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 100 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
100 | 101 |
101 // Send the multipart response. No image or data buffer is created. | 102 // Send the multipart response. No image or data buffer is created. |
102 // Note that the response must be routed through ResourceLoader to | 103 // Note that the response must be routed through ResourceLoader to |
103 // ensure the load is flagged as multipart. | 104 // ensure the load is flagged as multipart. |
104 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, n
ullAtom, String()); | 105 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, n
ullAtom, String()); |
105 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(m
ultipartResponse), nullptr); | 106 cachedImage->loader()->didReceiveResponse(nullptr, WrappedResourceResponse(m
ultipartResponse), nullptr); |
106 ASSERT_FALSE(cachedImage->resourceBuffer()); | 107 ASSERT_FALSE(cachedImage->resourceBuffer()); |
107 ASSERT_FALSE(cachedImage->hasImage()); | 108 ASSERT_FALSE(cachedImage->hasImage()); |
108 ASSERT_EQ(client.imageChangedCount(), 0); | 109 ASSERT_EQ(client.imageChangedCount(), 0); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 146 |
146 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); | 147 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); |
147 | 148 |
148 // Emulate starting a real load. | 149 // Emulate starting a real load. |
149 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); | 150 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); |
150 cachedImage->setIdentifier(createUniqueIdentifier()); | 151 cachedImage->setIdentifier(createUniqueIdentifier()); |
151 | 152 |
152 cachedImage->load(fetcher, ResourceLoaderOptions()); | 153 cachedImage->load(fetcher, ResourceLoaderOptions()); |
153 memoryCache()->add(cachedImage.get()); | 154 memoryCache()->add(cachedImage.get()); |
154 | 155 |
155 MockImageResourceClient client; | 156 MockImageResourceClient client(cachedImage); |
156 cachedImage->addClient(&client); | |
157 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 157 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
158 | 158 |
159 // The load should still be alive, but a timer should be started to cancel t
he load inside removeClient(). | 159 // The load should still be alive, but a timer should be started to cancel t
he load inside removeClient(). |
160 cachedImage->removeClient(&client); | 160 client.removeAsClient(); |
161 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 161 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
162 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); | 162 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); |
163 | 163 |
164 // Trigger the cancel timer, ensure the load was cancelled and the resource
was evicted from the cache. | 164 // Trigger the cancel timer, ensure the load was cancelled and the resource
was evicted from the cache. |
165 blink::testing::runPendingTasks(); | 165 blink::testing::runPendingTasks(); |
166 EXPECT_EQ(Resource::LoadError, cachedImage->status()); | 166 EXPECT_EQ(Resource::LoadError, cachedImage->status()); |
167 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); | 167 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); |
168 | 168 |
169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | 169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); |
170 } | 170 } |
171 | 171 |
172 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) | 172 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) |
173 { | 173 { |
174 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); | 174 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); |
175 cachedImage->setLoading(true); | 175 cachedImage->setLoading(true); |
176 | 176 |
177 MockImageResourceClient client; | 177 MockImageResourceClient client(cachedImage); |
178 cachedImage->addClient(&client); | |
179 | 178 |
180 // Send the image response. | 179 // Send the image response. |
181 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String()), nullptr); | 180 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String()), nullptr); |
182 | 181 |
183 Vector<unsigned char> jpeg = jpegImage(); | 182 Vector<unsigned char> jpeg = jpegImage(); |
184 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | 183 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); |
185 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); | 184 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); |
186 cachedImage->finish(); | 185 cachedImage->finish(); |
187 ASSERT_FALSE(cachedImage->errorOccurred()); | 186 ASSERT_FALSE(cachedImage->errorOccurred()); |
188 ASSERT_TRUE(cachedImage->hasImage()); | 187 ASSERT_TRUE(cachedImage->hasImage()); |
189 ASSERT_FALSE(cachedImage->image()->isNull()); | 188 ASSERT_FALSE(cachedImage->image()->isNull()); |
190 ASSERT_TRUE(client.notifyFinishedCalled()); | 189 ASSERT_TRUE(client.notifyFinishedCalled()); |
191 | 190 |
192 // The prune comes when the ImageResource still has clients. The image shoul
d not be deleted. | 191 // The prune comes when the ImageResource still has clients. The image shoul
d not be deleted. |
193 cachedImage->prune(); | 192 cachedImage->prune(); |
194 ASSERT_TRUE(cachedImage->hasClients()); | 193 ASSERT_TRUE(cachedImage->hasClients()); |
195 ASSERT_TRUE(cachedImage->hasImage()); | 194 ASSERT_TRUE(cachedImage->hasImage()); |
196 ASSERT_FALSE(cachedImage->image()->isNull()); | 195 ASSERT_FALSE(cachedImage->image()->isNull()); |
197 | 196 |
198 // The ImageResource no longer has clients. The image should be deleted by p
rune. | 197 // The ImageResource no longer has clients. The image should be deleted by p
rune. |
199 cachedImage->removeClient(&client); | 198 client.removeAsClient(); |
200 cachedImage->prune(); | 199 cachedImage->prune(); |
201 ASSERT_FALSE(cachedImage->hasClients()); | 200 ASSERT_FALSE(cachedImage->hasClients()); |
202 ASSERT_FALSE(cachedImage->hasImage()); | 201 ASSERT_FALSE(cachedImage->hasImage()); |
203 ASSERT_TRUE(cachedImage->image()->isNull()); | 202 ASSERT_TRUE(cachedImage->image()->isNull()); |
204 } | 203 } |
205 | 204 |
206 TEST(ImageResourceTest, UpdateBitmapImages) | 205 TEST(ImageResourceTest, UpdateBitmapImages) |
207 { | 206 { |
208 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); | 207 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); |
209 cachedImage->setLoading(true); | 208 cachedImage->setLoading(true); |
210 | 209 |
211 MockImageResourceClient client; | 210 MockImageResourceClient client(cachedImage); |
212 cachedImage->addClient(&client); | |
213 | 211 |
214 // Send the image response. | 212 // Send the image response. |
215 Vector<unsigned char> jpeg = jpegImage(); | 213 Vector<unsigned char> jpeg = jpegImage(); |
216 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | 214 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); |
217 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); | 215 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); |
218 cachedImage->finish(); | 216 cachedImage->finish(); |
219 ASSERT_FALSE(cachedImage->errorOccurred()); | 217 ASSERT_FALSE(cachedImage->errorOccurred()); |
220 ASSERT_TRUE(cachedImage->hasImage()); | 218 ASSERT_TRUE(cachedImage->hasImage()); |
221 ASSERT_FALSE(cachedImage->image()->isNull()); | 219 ASSERT_FALSE(cachedImage->image()->isNull()); |
222 ASSERT_EQ(client.imageChangedCount(), 2); | 220 ASSERT_EQ(client.imageChangedCount(), 2); |
223 ASSERT_TRUE(client.notifyFinishedCalled()); | 221 ASSERT_TRUE(client.notifyFinishedCalled()); |
224 | 222 |
225 HashSet<ImageResource*> bitmapImages; | 223 HashSet<ImageResource*> bitmapImages; |
226 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); | 224 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); |
227 bitmapImages.add(cachedImage.get()); | 225 bitmapImages.add(cachedImage.get()); |
228 } | 226 } |
229 | 227 |
230 } // namespace blink | 228 } // namespace blink |
OLD | NEW |