Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp

Issue 1667843003: Make Resource RefCountedWillBeGarbageCollectedFinalized, attempt #2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase + address review comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 17 matching lines...) Expand all
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/fetch/ImageResource.h" 31 #include "core/fetch/ImageResource.h"
32 32
33 #include "core/fetch/ImageResourceClient.h" 33 #include "core/fetch/ImageResourceClient.h"
34 #include "core/fetch/MemoryCache.h" 34 #include "core/fetch/MemoryCache.h"
35 #include "core/fetch/MockImageResourceClient.h" 35 #include "core/fetch/MockImageResourceClient.h"
36 #include "core/fetch/ResourceFetcher.h" 36 #include "core/fetch/ResourceFetcher.h"
37 #include "core/fetch/ResourceLoader.h" 37 #include "core/fetch/ResourceLoader.h"
38 #include "core/fetch/ResourcePtr.h"
39 #include "core/fetch/UniqueIdentifier.h" 38 #include "core/fetch/UniqueIdentifier.h"
40 #include "platform/SharedBuffer.h" 39 #include "platform/SharedBuffer.h"
41 #include "platform/exported/WrappedResourceResponse.h" 40 #include "platform/exported/WrappedResourceResponse.h"
42 #include "platform/graphics/Image.h" 41 #include "platform/graphics/Image.h"
43 #include "platform/testing/URLTestHelpers.h" 42 #include "platform/testing/URLTestHelpers.h"
44 #include "platform/testing/UnitTestHelpers.h" 43 #include "platform/testing/UnitTestHelpers.h"
45 #include "public/platform/Platform.h" 44 #include "public/platform/Platform.h"
46 #include "public/platform/WebURL.h" 45 #include "public/platform/WebURL.h"
47 #include "public/platform/WebURLResponse.h" 46 #include "public/platform/WebURLResponse.h"
48 #include "public/platform/WebUnitTestSupport.h" 47 #include "public/platform/WebUnitTestSupport.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 return jpeg; 81 return jpeg;
83 } 82 }
84 83
85 TEST(ImageResourceTest, MultipartImage) 84 TEST(ImageResourceTest, MultipartImage)
86 { 85 {
87 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); 86 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr);
88 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 87 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
89 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 88 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
90 89
91 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli ent callbacks. 90 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli ent callbacks.
92 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t estURL), nullptr); 91 RefPtrWillBeRawPtr<ImageResource> cachedImage = ImageResource::create(Resour ceRequest(testURL), nullptr);
93 cachedImage->setIdentifier(createUniqueIdentifier()); 92 cachedImage->setIdentifier(createUniqueIdentifier());
94 cachedImage->load(fetcher, ResourceLoaderOptions()); 93 cachedImage->load(fetcher, ResourceLoaderOptions());
95 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); 94 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
96 95
97 MockImageResourceClient client(cachedImage); 96 MockImageResourceClient client(cachedImage);
98 EXPECT_EQ(Resource::Pending, cachedImage->status()); 97 EXPECT_EQ(Resource::Pending, cachedImage->status());
99 98
100 // Send the multipart response. No image or data buffer is created. 99 // Send the multipart response. No image or data buffer is created.
101 // Note that the response must be routed through ResourceLoader to 100 // Note that the response must be routed through ResourceLoader to
102 // ensure the load is flagged as multipart. 101 // ensure the load is flagged as multipart.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 137 }
139 138
140 TEST(ImageResourceTest, CancelOnDetach) 139 TEST(ImageResourceTest, CancelOnDetach)
141 { 140 {
142 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 141 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
143 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 142 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
144 143
145 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); 144 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr);
146 145
147 // Emulate starting a real load. 146 // Emulate starting a real load.
148 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t estURL), nullptr); 147 RefPtrWillBeRawPtr<ImageResource> cachedImage = ImageResource::create(Resour ceRequest(testURL), nullptr);
149 cachedImage->setIdentifier(createUniqueIdentifier()); 148 cachedImage->setIdentifier(createUniqueIdentifier());
150 149
151 cachedImage->load(fetcher, ResourceLoaderOptions()); 150 cachedImage->load(fetcher, ResourceLoaderOptions());
152 memoryCache()->add(cachedImage.get()); 151 memoryCache()->add(cachedImage.get());
153 152
154 MockImageResourceClient client(cachedImage); 153 MockImageResourceClient client(cachedImage);
155 EXPECT_EQ(Resource::Pending, cachedImage->status()); 154 EXPECT_EQ(Resource::Pending, cachedImage->status());
156 155
157 // The load should still be alive, but a timer should be started to cancel t he load inside removeClient(). 156 // The load should still be alive, but a timer should be started to cancel t he load inside removeClient().
158 client.removeAsClient(); 157 client.removeAsClient();
159 EXPECT_EQ(Resource::Pending, cachedImage->status()); 158 EXPECT_EQ(Resource::Pending, cachedImage->status());
160 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL)); 159 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL));
161 160
162 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache. 161 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
163 blink::testing::runPendingTasks(); 162 blink::testing::runPendingTasks();
164 EXPECT_EQ(Resource::LoadError, cachedImage->status()); 163 EXPECT_EQ(Resource::LoadError, cachedImage->status());
165 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL)); 164 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test URL));
166 165
167 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); 166 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
168 } 167 }
169 168
170 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) 169 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients)
171 { 170 {
172 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest() , nullptr); 171 RefPtrWillBeRawPtr<ImageResource> cachedImage = ImageResource::create(Resour ceRequest(), nullptr);
173 cachedImage->setLoading(true); 172 cachedImage->setLoading(true);
174 173
175 MockImageResourceClient client(cachedImage); 174 MockImageResourceClient client(cachedImage);
176 175
177 // Send the image response. 176 // Send the image response.
178 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, nullAtom, String()), nullptr); 177 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, nullAtom, String()), nullptr);
179 178
180 Vector<unsigned char> jpeg = jpegImage(); 179 Vector<unsigned char> jpeg = jpegImage();
181 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr); 180 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr);
182 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 181 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
(...skipping 12 matching lines...) Expand all
195 // The ImageResource no longer has clients. The image should be deleted by p rune. 194 // The ImageResource no longer has clients. The image should be deleted by p rune.
196 client.removeAsClient(); 195 client.removeAsClient();
197 cachedImage->prune(); 196 cachedImage->prune();
198 ASSERT_FALSE(cachedImage->hasClients()); 197 ASSERT_FALSE(cachedImage->hasClients());
199 ASSERT_FALSE(cachedImage->hasImage()); 198 ASSERT_FALSE(cachedImage->hasImage());
200 ASSERT_TRUE(cachedImage->image()->isNull()); 199 ASSERT_TRUE(cachedImage->image()->isNull());
201 } 200 }
202 201
203 TEST(ImageResourceTest, UpdateBitmapImages) 202 TEST(ImageResourceTest, UpdateBitmapImages)
204 { 203 {
205 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest() , nullptr); 204 RefPtrWillBeRawPtr<ImageResource> cachedImage = ImageResource::create(Resour ceRequest(), nullptr);
206 cachedImage->setLoading(true); 205 cachedImage->setLoading(true);
207 206
208 MockImageResourceClient client(cachedImage); 207 MockImageResourceClient client(cachedImage);
209 208
210 // Send the image response. 209 // Send the image response.
211 Vector<unsigned char> jpeg = jpegImage(); 210 Vector<unsigned char> jpeg = jpegImage();
212 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr); 211 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si ze(), nullAtom, String()), nullptr);
213 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 212 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
214 cachedImage->finish(); 213 cachedImage->finish();
215 ASSERT_FALSE(cachedImage->errorOccurred()); 214 ASSERT_FALSE(cachedImage->errorOccurred());
216 ASSERT_TRUE(cachedImage->hasImage()); 215 ASSERT_TRUE(cachedImage->hasImage());
217 ASSERT_FALSE(cachedImage->image()->isNull()); 216 ASSERT_FALSE(cachedImage->image()->isNull());
218 ASSERT_EQ(client.imageChangedCount(), 2); 217 ASSERT_EQ(client.imageChangedCount(), 2);
219 ASSERT_TRUE(client.notifyFinishedCalled()); 218 ASSERT_TRUE(client.notifyFinishedCalled());
220 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); 219 ASSERT_TRUE(cachedImage->image()->isBitmapImage());
221 } 220 }
222 221
223 TEST(ImageResourceTest, ReloadIfLoFi) 222 TEST(ImageResourceTest, ReloadIfLoFi)
224 { 223 {
225 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
226 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html "); 225 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html ");
227 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t estURL), nullptr); 226 RefPtrWillBeRawPtr<ImageResource> cachedImage = ImageResource::create(Resour ceRequest(testURL), nullptr);
228 cachedImage->setLoading(true); 227 cachedImage->setLoading(true);
229 228
230 MockImageResourceClient client(cachedImage); 229 MockImageResourceClient client(cachedImage);
231 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr); 230 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr);
232 231
233 // Send the image response. 232 // Send the image response.
234 Vector<unsigned char> jpeg = jpegImage(); 233 Vector<unsigned char> jpeg = jpegImage();
235 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAto m, String()); 234 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAto m, String());
236 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 235 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
237 236
(...skipping 17 matching lines...) Expand all
255 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e()); 254 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz e());
256 cachedImage->finish(); 255 cachedImage->finish();
257 ASSERT_FALSE(cachedImage->errorOccurred()); 256 ASSERT_FALSE(cachedImage->errorOccurred());
258 ASSERT_TRUE(cachedImage->hasImage()); 257 ASSERT_TRUE(cachedImage->hasImage());
259 ASSERT_FALSE(cachedImage->image()->isNull()); 258 ASSERT_FALSE(cachedImage->image()->isNull());
260 ASSERT_TRUE(client.notifyFinishedCalled()); 259 ASSERT_TRUE(client.notifyFinishedCalled());
261 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); 260 ASSERT_TRUE(cachedImage->image()->isBitmapImage());
262 } 261 }
263 262
264 } // namespace blink 263 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/ImageResource.cpp ('k') | third_party/WebKit/Source/core/fetch/LinkFetchResource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698