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

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

Issue 2407573002: Wait to notify completion until after a Lo-Fi image is reloaded. (Closed)
Patch Set: removed unnecessary m_isSchedulingReload check Created 4 years, 2 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 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
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/FetchInitiatorInfo.h"
34 #include "core/fetch/FetchRequest.h"
33 #include "core/fetch/MemoryCache.h" 35 #include "core/fetch/MemoryCache.h"
34 #include "core/fetch/MockResourceClients.h" 36 #include "core/fetch/MockResourceClients.h"
35 #include "core/fetch/ResourceFetcher.h" 37 #include "core/fetch/ResourceFetcher.h"
36 #include "core/fetch/ResourceLoader.h" 38 #include "core/fetch/ResourceLoader.h"
37 #include "core/fetch/UniqueIdentifier.h" 39 #include "core/fetch/UniqueIdentifier.h"
38 #include "platform/SharedBuffer.h" 40 #include "platform/SharedBuffer.h"
39 #include "platform/exported/WrappedResourceResponse.h" 41 #include "platform/exported/WrappedResourceResponse.h"
40 #include "platform/graphics/Image.h" 42 #include "platform/graphics/Image.h"
41 #include "platform/scheduler/test/fake_web_task_runner.h" 43 #include "platform/scheduler/test/fake_web_task_runner.h"
42 #include "platform/testing/URLTestHelpers.h" 44 #include "platform/testing/URLTestHelpers.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 const Vector<unsigned char>& data) { 157 const Vector<unsigned char>& data) {
156 ResourceResponse response; 158 ResourceResponse response;
157 response.setURL(url); 159 response.setURL(url);
158 response.setHTTPStatusCode(200); 160 response.setHTTPStatusCode(200);
159 response.setMimeType(mimeType); 161 response.setMimeType(mimeType);
160 imageResource->responseReceived(response, nullptr); 162 imageResource->responseReceived(response, nullptr);
161 imageResource->appendData(reinterpret_cast<const char*>(data.data()), 163 imageResource->appendData(reinterpret_cast<const char*>(data.data()),
162 data.size()); 164 data.size());
163 imageResource->finish(); 165 imageResource->finish();
164 } 166 }
165 }
166 167
167 class ImageResourceTestMockFetchContext : public FetchContext { 168 class ImageResourceTestMockFetchContext : public FetchContext {
168 public: 169 public:
169 static ImageResourceTestMockFetchContext* create() { 170 static ImageResourceTestMockFetchContext* create() {
170 return new ImageResourceTestMockFetchContext; 171 return new ImageResourceTestMockFetchContext;
171 } 172 }
172 173
173 virtual ~ImageResourceTestMockFetchContext() {} 174 virtual ~ImageResourceTestMockFetchContext() {}
174 175
175 bool allowImage(bool imagesEnabled, const KURL&) const override { 176 bool allowImage(bool imagesEnabled, const KURL&) const override {
(...skipping 10 matching lines...) Expand all
186 bool shouldLoadNewResource(Resource::Type) const override { return true; } 187 bool shouldLoadNewResource(Resource::Type) const override { return true; }
187 WebTaskRunner* loadingTaskRunner() const override { return m_runner.get(); } 188 WebTaskRunner* loadingTaskRunner() const override { return m_runner.get(); }
188 189
189 private: 190 private:
190 ImageResourceTestMockFetchContext() 191 ImageResourceTestMockFetchContext()
191 : m_runner(wrapUnique(new scheduler::FakeWebTaskRunner)) {} 192 : m_runner(wrapUnique(new scheduler::FakeWebTaskRunner)) {}
192 193
193 std::unique_ptr<scheduler::FakeWebTaskRunner> m_runner; 194 std::unique_ptr<scheduler::FakeWebTaskRunner> m_runner;
194 }; 195 };
195 196
197 // Convenience class that registers a mocked URL load on construction, and
198 // unregisters it on destruction. This allows for a test to use constructs like
199 // ASSERT_TRUE() without needing to worry about unregistering the mocked URL
200 // load to avoid putting other tests into inconsistent states in case the
201 // assertion fails.
202 class ScopedRegisteredURL {
203 public:
204 ScopedRegisteredURL(const KURL& url,
205 const String& fileName = "cancelTest.html",
206 const String& mimeType = "text/html")
207 : m_url(url) {
208 URLTestHelpers::registerMockedURLLoad(m_url, fileName, mimeType);
209 }
210
211 ~ScopedRegisteredURL() {
212 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url);
213 }
214
215 private:
216 KURL m_url;
217 };
218
219 } // namespace
220
196 TEST(ImageResourceTest, MultipartImage) { 221 TEST(ImageResourceTest, MultipartImage) {
197 ResourceFetcher* fetcher = 222 ResourceFetcher* fetcher =
198 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 223 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
199 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 224 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
200 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 225 ScopedRegisteredURL scopedRegisteredURL(testURL);
201 "text/html");
202 226
203 // Emulate starting a real load, but don't expect any "real" 227 // Emulate starting a real load, but don't expect any "real"
204 // WebURLLoaderClient callbacks. 228 // WebURLLoaderClient callbacks.
205 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); 229 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL));
206 cachedImage->setIdentifier(createUniqueIdentifier()); 230 cachedImage->setIdentifier(createUniqueIdentifier());
207 fetcher->startLoad(cachedImage); 231 fetcher->startLoad(cachedImage);
208 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL);
209 232
210 Persistent<MockImageResourceClient> client = 233 Persistent<MockImageResourceClient> client =
211 new MockImageResourceClient(cachedImage); 234 new MockImageResourceClient(cachedImage);
212 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 235 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
213 236
214 // Send the multipart response. No image or data buffer is created. Note that 237 // Send the multipart response. No image or data buffer is created. Note that
215 // the response must be routed through ResourceLoader to ensure the load is 238 // the response must be routed through ResourceLoader to ensure the load is
216 // flagged as multipart. 239 // flagged as multipart.
217 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0, 240 ResourceResponse multipartResponse(KURL(), "multipart/x-mixed-replace", 0,
218 nullAtom, String()); 241 nullAtom, String());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 EXPECT_EQ(1, cachedImage->getImage()->width()); 293 EXPECT_EQ(1, cachedImage->getImage()->width());
271 EXPECT_EQ(1, cachedImage->getImage()->height()); 294 EXPECT_EQ(1, cachedImage->getImage()->height());
272 EXPECT_EQ(1, client->imageChangedCount()); 295 EXPECT_EQ(1, client->imageChangedCount());
273 EXPECT_TRUE(client->notifyFinishedCalled()); 296 EXPECT_TRUE(client->notifyFinishedCalled());
274 EXPECT_EQ(1, client2->imageChangedCount()); 297 EXPECT_EQ(1, client2->imageChangedCount());
275 EXPECT_TRUE(client2->notifyFinishedCalled()); 298 EXPECT_TRUE(client2->notifyFinishedCalled());
276 } 299 }
277 300
278 TEST(ImageResourceTest, CancelOnDetach) { 301 TEST(ImageResourceTest, CancelOnDetach) {
279 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 302 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
280 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 303 ScopedRegisteredURL scopedRegisteredURL(testURL);
281 "text/html");
282 304
283 ResourceFetcher* fetcher = 305 ResourceFetcher* fetcher =
284 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 306 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
285 307
286 // Emulate starting a real load. 308 // Emulate starting a real load.
287 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL)); 309 ImageResource* cachedImage = ImageResource::create(ResourceRequest(testURL));
288 cachedImage->setIdentifier(createUniqueIdentifier()); 310 cachedImage->setIdentifier(createUniqueIdentifier());
289 311
290 fetcher->startLoad(cachedImage); 312 fetcher->startLoad(cachedImage);
291 memoryCache()->add(cachedImage); 313 memoryCache()->add(cachedImage);
292 314
293 Persistent<MockImageResourceClient> client = 315 Persistent<MockImageResourceClient> client =
294 new MockImageResourceClient(cachedImage); 316 new MockImageResourceClient(cachedImage);
295 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 317 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
296 318
297 // The load should still be alive, but a timer should be started to cancel the 319 // The load should still be alive, but a timer should be started to cancel the
298 // load inside removeClient(). 320 // load inside removeClient().
299 client->removeAsClient(); 321 client->removeAsClient();
300 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 322 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
301 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); 323 EXPECT_TRUE(memoryCache()->resourceForURL(testURL));
302 324
303 // Trigger the cancel timer, ensure the load was cancelled and the resource 325 // Trigger the cancel timer, ensure the load was cancelled and the resource
304 // was evicted from the cache. 326 // was evicted from the cache.
305 blink::testing::runPendingTasks(); 327 blink::testing::runPendingTasks();
306 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); 328 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus());
307 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); 329 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
308
309 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL);
310 } 330 }
311 331
312 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) { 332 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) {
313 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 333 ImageResource* cachedImage = ImageResource::create(ResourceRequest());
314 cachedImage->setStatus(Resource::Pending); 334 cachedImage->setStatus(Resource::Pending);
315 335
316 Persistent<MockImageResourceClient> client = 336 Persistent<MockImageResourceClient> client =
317 new MockImageResourceClient(cachedImage); 337 new MockImageResourceClient(cachedImage);
318 338
319 // Send the image response. 339 // Send the image response.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 jpeg.size()); 387 jpeg.size());
368 cachedImage->finish(); 388 cachedImage->finish();
369 EXPECT_FALSE(cachedImage->errorOccurred()); 389 EXPECT_FALSE(cachedImage->errorOccurred());
370 ASSERT_TRUE(cachedImage->hasImage()); 390 ASSERT_TRUE(cachedImage->hasImage());
371 EXPECT_FALSE(cachedImage->getImage()->isNull()); 391 EXPECT_FALSE(cachedImage->getImage()->isNull());
372 EXPECT_EQ(2, client->imageChangedCount()); 392 EXPECT_EQ(2, client->imageChangedCount());
373 EXPECT_TRUE(client->notifyFinishedCalled()); 393 EXPECT_TRUE(client->notifyFinishedCalled());
374 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); 394 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
375 } 395 }
376 396
377 TEST(ImageResourceTest, ReloadIfLoFi) { 397 TEST(ImageResourceTest, ReloadIfLoFiAfterFinished) {
378 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 398 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
379 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 399 ScopedRegisteredURL scopedRegisteredURL(testURL);
380 "text/html");
381 ResourceRequest request = ResourceRequest(testURL); 400 ResourceRequest request = ResourceRequest(testURL);
382 request.setLoFiState(WebURLRequest::LoFiOn); 401 request.setLoFiState(WebURLRequest::LoFiOn);
383 ImageResource* cachedImage = ImageResource::create(request); 402 ImageResource* cachedImage = ImageResource::create(request);
384 cachedImage->setStatus(Resource::Pending); 403 cachedImage->setStatus(Resource::Pending);
385 404
386 Persistent<MockImageResourceClient> client = 405 Persistent<MockImageResourceClient> client =
387 new MockImageResourceClient(cachedImage); 406 new MockImageResourceClient(cachedImage);
388 ResourceFetcher* fetcher = 407 ResourceFetcher* fetcher =
389 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 408 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
390 409
391 // Send the image response. 410 // Send the image response.
392 Vector<unsigned char> jpeg = jpegImage(); 411 Vector<unsigned char> jpeg = jpegImage();
393 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom, 412 ResourceResponse resourceResponse(KURL(), "image/jpeg", jpeg.size(), nullAtom,
394 String()); 413 String());
395 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 414 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
396 415
397 cachedImage->responseReceived(resourceResponse, nullptr); 416 cachedImage->responseReceived(resourceResponse, nullptr);
398 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), 417 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()),
399 jpeg.size()); 418 jpeg.size());
400 cachedImage->finish(); 419 cachedImage->finish();
401 EXPECT_FALSE(cachedImage->errorOccurred()); 420 EXPECT_FALSE(cachedImage->errorOccurred());
402 ASSERT_TRUE(cachedImage->hasImage()); 421 ASSERT_TRUE(cachedImage->hasImage());
403 EXPECT_FALSE(cachedImage->getImage()->isNull()); 422 EXPECT_FALSE(cachedImage->getImage()->isNull());
404 EXPECT_EQ(2, client->imageChangedCount()); 423 EXPECT_EQ(2, client->imageChangedCount());
424 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged());
425 // The client should have been notified that the image load completed.
405 EXPECT_TRUE(client->notifyFinishedCalled()); 426 EXPECT_TRUE(client->notifyFinishedCalled());
427 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished());
428 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished());
406 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); 429 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
407 EXPECT_EQ(1, cachedImage->getImage()->width()); 430 EXPECT_EQ(1, cachedImage->getImage()->width());
408 EXPECT_EQ(1, cachedImage->getImage()->height()); 431 EXPECT_EQ(1, cachedImage->getImage()->height());
409 432
433 // Call reloadIfLoFi() after the image has finished loading.
410 cachedImage->reloadIfLoFi(fetcher); 434 cachedImage->reloadIfLoFi(fetcher);
411 EXPECT_FALSE(cachedImage->errorOccurred()); 435 EXPECT_FALSE(cachedImage->errorOccurred());
412 EXPECT_FALSE(cachedImage->resourceBuffer()); 436 EXPECT_FALSE(cachedImage->resourceBuffer());
413 EXPECT_FALSE(cachedImage->hasImage()); 437 EXPECT_FALSE(cachedImage->hasImage());
414 EXPECT_EQ(3, client->imageChangedCount()); 438 EXPECT_EQ(3, client->imageChangedCount());
415 439
416 Vector<unsigned char> jpeg2 = jpegImage2(); 440 Vector<unsigned char> jpeg2 = jpegImage2();
417 cachedImage->loader()->didReceiveResponse( 441 cachedImage->loader()->didReceiveResponse(
418 nullptr, WrappedResourceResponse(resourceResponse), nullptr); 442 nullptr, WrappedResourceResponse(resourceResponse), nullptr);
419 cachedImage->loader()->didReceiveData( 443 cachedImage->loader()->didReceiveData(
420 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(), 444 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(),
421 jpeg2.size(), jpeg2.size()); 445 jpeg2.size(), jpeg2.size());
422 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size()); 446 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size());
423 EXPECT_FALSE(cachedImage->errorOccurred()); 447 EXPECT_FALSE(cachedImage->errorOccurred());
424 ASSERT_TRUE(cachedImage->hasImage()); 448 ASSERT_TRUE(cachedImage->hasImage());
425 EXPECT_FALSE(cachedImage->getImage()->isNull()); 449 EXPECT_FALSE(cachedImage->getImage()->isNull());
450 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged());
426 EXPECT_TRUE(client->notifyFinishedCalled()); 451 EXPECT_TRUE(client->notifyFinishedCalled());
452 // The client should not have been notified of completion again.
453 EXPECT_EQ(jpeg.size(), client->encodedSizeOnNotifyFinished());
454 EXPECT_EQ(jpeg.size(), client->encodedSizeOnImageNotifyFinished());
427 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); 455 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
428 EXPECT_EQ(50, cachedImage->getImage()->width()); 456 EXPECT_EQ(50, cachedImage->getImage()->width());
429 EXPECT_EQ(50, cachedImage->getImage()->height()); 457 EXPECT_EQ(50, cachedImage->getImage()->height());
458 }
459
460 TEST(ImageResourceTest, ReloadIfLoFiDuringFetch) {
461 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
462 ScopedRegisteredURL scopedRegisteredURL(testURL);
463
464 ResourceRequest request(testURL);
465 request.setLoFiState(WebURLRequest::LoFiOn);
466 FetchRequest fetchRequest(request, FetchInitiatorInfo());
467 ResourceFetcher* fetcher =
468 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
469
470 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher);
471 Persistent<MockImageResourceClient> client =
472 new MockImageResourceClient(cachedImage);
473
474 // Send the image response.
475 Vector<unsigned char> jpeg = jpegImage();
476
477 ResourceResponse initialResourceResponse(testURL, "image/jpeg", jpeg.size(),
478 nullAtom, String());
479 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
480
481 cachedImage->loader()->didReceiveResponse(
482 nullptr, WrappedResourceResponse(initialResourceResponse));
483 cachedImage->loader()->didReceiveData(
484 nullptr, reinterpret_cast<const char*>(jpeg.data()), jpeg.size(),
485 jpeg.size(), jpeg.size());
486
487 EXPECT_FALSE(cachedImage->errorOccurred());
488 ASSERT_TRUE(cachedImage->hasImage());
489 EXPECT_FALSE(cachedImage->getImage()->isNull());
490 EXPECT_EQ(1, client->imageChangedCount());
491 EXPECT_EQ(jpeg.size(), client->encodedSizeOnLastImageChanged());
492 EXPECT_FALSE(client->notifyFinishedCalled());
493 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
494 EXPECT_EQ(1, cachedImage->getImage()->width());
495 EXPECT_EQ(1, cachedImage->getImage()->height());
496
497 // Call reloadIfLoFi() while the image is still loading.
498 cachedImage->reloadIfLoFi(fetcher);
499 EXPECT_FALSE(cachedImage->errorOccurred());
500 EXPECT_FALSE(cachedImage->resourceBuffer());
501 EXPECT_FALSE(cachedImage->hasImage());
502 EXPECT_EQ(2, client->imageChangedCount());
503 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged());
504 // The client should not have been notified of completion yet, since the image
505 // is still loading.
506 EXPECT_FALSE(client->notifyFinishedCalled());
507
508 Vector<unsigned char> jpeg2 = jpegImage2();
509 cachedImage->loader()->didReceiveResponse(
510 nullptr, WrappedResourceResponse(ResourceResponse(
511 testURL, "image/jpeg", jpeg.size(), nullAtom, String())),
512 nullptr);
513 cachedImage->loader()->didReceiveData(
514 nullptr, reinterpret_cast<const char*>(jpeg2.data()), jpeg2.size(),
515 jpeg2.size(), jpeg2.size());
516 cachedImage->loader()->didFinishLoading(nullptr, 0.0, jpeg2.size());
517
518 EXPECT_FALSE(cachedImage->errorOccurred());
519 ASSERT_TRUE(cachedImage->hasImage());
520 EXPECT_FALSE(cachedImage->getImage()->isNull());
521 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnLastImageChanged());
522 // The client should have been notified of completion only after the reload
523 // completed.
524 EXPECT_TRUE(client->notifyFinishedCalled());
525 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnNotifyFinished());
526 EXPECT_EQ(jpeg2.size(), client->encodedSizeOnImageNotifyFinished());
527 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
528 EXPECT_EQ(50, cachedImage->getImage()->width());
529 EXPECT_EQ(50, cachedImage->getImage()->height());
430 } 530 }
431 531
432 TEST(ImageResourceTest, SVGImage) { 532 TEST(ImageResourceTest, SVGImage) {
433 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); 533 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo");
434 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); 534 ImageResource* imageResource = ImageResource::create(ResourceRequest(url));
435 Persistent<MockImageResourceClient> client = 535 Persistent<MockImageResourceClient> client =
436 new MockImageResourceClient(imageResource); 536 new MockImageResourceClient(imageResource);
437 537
438 receiveResponse(imageResource, url, "image/svg+xml", svgImage()); 538 receiveResponse(imageResource, url, "image/svg+xml", svgImage());
439 539
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 765
666 ASSERT_TRUE(imageResource->hasImage()); 766 ASSERT_TRUE(imageResource->hasImage());
667 EXPECT_FALSE(imageResource->getImage()->isNull()); 767 EXPECT_FALSE(imageResource->getImage()->isNull());
668 EXPECT_EQ(1, imageResource->getImage()->width()); 768 EXPECT_EQ(1, imageResource->getImage()->width());
669 EXPECT_EQ(1, imageResource->getImage()->height()); 769 EXPECT_EQ(1, imageResource->getImage()->height());
670 EXPECT_TRUE(client2->notifyFinishedCalled()); 770 EXPECT_TRUE(client2->notifyFinishedCalled());
671 } 771 }
672 772
673 TEST(ImageResourceTest, CancelOnDecodeError) { 773 TEST(ImageResourceTest, CancelOnDecodeError) {
674 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 774 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
675 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 775 ScopedRegisteredURL scopedRegisteredURL(testURL);
676 "text/html");
677 776
678 ResourceFetcher* fetcher = 777 ResourceFetcher* fetcher =
679 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 778 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
680 FetchRequest request(testURL, FetchInitiatorInfo()); 779 FetchRequest request(testURL, FetchInitiatorInfo());
681 ImageResource* cachedImage = ImageResource::fetch(request, fetcher); 780 ImageResource* cachedImage = ImageResource::fetch(request, fetcher);
682 Platform::current()->getURLLoaderMockFactory()->unregisterURL(testURL);
683 781
684 cachedImage->loader()->didReceiveResponse( 782 cachedImage->loader()->didReceiveResponse(
685 nullptr, WrappedResourceResponse(ResourceResponse( 783 nullptr, WrappedResourceResponse(ResourceResponse(
686 testURL, "image/jpeg", 18, nullAtom, String())), 784 testURL, "image/jpeg", 18, nullAtom, String())),
687 nullptr); 785 nullptr);
688 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18, 786 cachedImage->loader()->didReceiveData(nullptr, "notactuallyanimage", 18, 18,
689 18); 787 18);
690 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus()); 788 EXPECT_EQ(Resource::DecodeError, cachedImage->getStatus());
691 EXPECT_FALSE(cachedImage->isLoading()); 789 EXPECT_FALSE(cachedImage->isLoading());
692 } 790 }
693 791
694 } // namespace blink 792 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/ImageResource.cpp ('k') | third_party/WebKit/Source/core/fetch/MockResourceClients.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698