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

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

Issue 2527353002: Phase II Step 3: Reload LoFi/placeholder images via new ImageResource
Patch Set: tests Created 4 years 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 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 309
310 fetcher->startLoad(cachedImage); 310 fetcher->startLoad(cachedImage);
311 memoryCache()->add(cachedImage); 311 memoryCache()->add(cachedImage);
312 312
313 Persistent<MockImageResourceClient> client = 313 Persistent<MockImageResourceClient> client =
314 new MockImageResourceClient(cachedImage); 314 new MockImageResourceClient(cachedImage);
315 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 315 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
316 316
317 // The load should still be alive, but a timer should be started to cancel the 317 // The load should still be alive, but a timer should be started to cancel the
318 // load inside removeClient(). 318 // load inside removeClient().
319 client->removeAsClient(); 319 client->removeAsClientAndObserver();
320 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); 320 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
321 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); 321 EXPECT_TRUE(memoryCache()->resourceForURL(testURL));
322 322
323 // Trigger the cancel timer, ensure the load was cancelled and the resource 323 // Trigger the cancel timer, ensure the load was cancelled and the resource
324 // was evicted from the cache. 324 // was evicted from the cache.
325 blink::testing::runPendingTasks(); 325 blink::testing::runPendingTasks();
326 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); 326 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus());
327 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); 327 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
328 } 328 }
329 329
(...skipping 26 matching lines...) Expand all
356 356
357 // The prune comes when the ImageResource still has clients. The image should 357 // The prune comes when the ImageResource still has clients. The image should
358 // not be deleted. 358 // not be deleted.
359 cachedImage->prune(); 359 cachedImage->prune();
360 EXPECT_TRUE(cachedImage->isAlive()); 360 EXPECT_TRUE(cachedImage->isAlive());
361 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 361 ASSERT_TRUE(cachedImage->getContent()->hasImage());
362 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 362 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
363 363
364 // The ImageResource no longer has clients. The decoded image data should be 364 // The ImageResource no longer has clients. The decoded image data should be
365 // deleted by prune. 365 // deleted by prune.
366 client->removeAsClient(); 366 client->removeAsClientAndObserver();
367 cachedImage->prune(); 367 cachedImage->prune();
368 EXPECT_FALSE(cachedImage->isAlive()); 368 EXPECT_FALSE(cachedImage->isAlive());
369 EXPECT_TRUE(cachedImage->getContent()->hasImage()); 369 EXPECT_TRUE(cachedImage->getContent()->hasImage());
370 // TODO(hajimehoshi): Should check cachedImage doesn't have decoded image 370 // TODO(hajimehoshi): Should check cachedImage doesn't have decoded image
371 // data. 371 // data.
372 } 372 }
373 373
374 TEST(ImageResourceTest, UpdateBitmapImages) { 374 TEST(ImageResourceTest, UpdateBitmapImages) {
375 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); 375 ImageResource* cachedImage = ImageResource::create(ResourceRequest());
376 cachedImage->setStatus(Resource::Pending); 376 cachedImage->setStatus(Resource::Pending);
(...skipping 11 matching lines...) Expand all
388 cachedImage->finish(); 388 cachedImage->finish();
389 EXPECT_FALSE(cachedImage->errorOccurred()); 389 EXPECT_FALSE(cachedImage->errorOccurred());
390 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 390 ASSERT_TRUE(cachedImage->getContent()->hasImage());
391 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 391 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
392 EXPECT_EQ(2, client->imageChangedCount()); 392 EXPECT_EQ(2, client->imageChangedCount());
393 EXPECT_TRUE(client->notifyFinishedCalled()); 393 EXPECT_TRUE(client->notifyFinishedCalled());
394 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 394 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
395 } 395 }
396 396
397 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { 397 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) {
398 ResourceFetcher* fetcher =
399 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
400
398 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 401 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
399 ScopedRegisteredURL scopedRegisteredURL(testURL); 402 ScopedRegisteredURL scopedRegisteredURL(testURL);
400 ResourceRequest request = ResourceRequest(testURL); 403 ResourceRequest request = ResourceRequest(testURL);
401 request.setLoFiState(WebURLRequest::LoFiOn); 404 request.setLoFiState(WebURLRequest::LoFiOn);
402 ImageResource* cachedImage = ImageResource::create(request); 405 ImageResource* cachedImage = ImageResource::create(request);
406 ImageResourceContent* content = cachedImage->getContent();
403 cachedImage->setStatus(Resource::Pending); 407 cachedImage->setStatus(Resource::Pending);
404 408
405 Persistent<MockImageResourceClient> client = 409 Persistent<MockImageResourceClient> client =
406 new MockImageResourceClient(cachedImage); 410 new MockImageResourceClient(cachedImage);
407 ResourceFetcher* fetcher =
408 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
409 411
410 // Send the image response. 412 // Send the image response.
411 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), 413 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage),
412 nullAtom, String()); 414 nullAtom, String());
413 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", 415 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform",
414 "empty-image"); 416 "empty-image");
415 417
416 cachedImage->responseReceived(resourceResponse, nullptr); 418 cachedImage->responseReceived(resourceResponse, nullptr);
417 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), 419 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage),
418 sizeof(kJpegImage)); 420 sizeof(kJpegImage));
419 cachedImage->finish(); 421 cachedImage->finish();
420 EXPECT_FALSE(cachedImage->errorOccurred()); 422 EXPECT_FALSE(content->errorOccurred());
421 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 423 ASSERT_TRUE(content->hasImage());
422 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 424 EXPECT_FALSE(content->getImage()->isNull());
423 EXPECT_EQ(2, client->imageChangedCount()); 425 EXPECT_EQ(2, client->imageChangedCount());
424 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 426 EXPECT_EQ(1, client->imageNotifyFinishedCount());
425 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 427 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
426 // The client should have been notified that the image load completed. 428 // The client should have been notified that the image load completed.
427 EXPECT_TRUE(client->notifyFinishedCalled()); 429 EXPECT_TRUE(client->notifyFinishedCalled());
428 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 430 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
429 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 431 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
430 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 432 EXPECT_TRUE(content->getImage()->isBitmapImage());
431 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); 433 EXPECT_EQ(1, content->getImage()->width());
432 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); 434 EXPECT_EQ(1, content->getImage()->height());
433 435
434 // Call reloadIfLoFiOrPlaceholder() after the image has finished loading. 436 // Call reloadIfLoFiOrPlaceholder() after the image has finished loading.
435 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); 437 ImageResource* reloadingResource =
436 EXPECT_FALSE(cachedImage->errorOccurred()); 438 cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
437 EXPECT_FALSE(cachedImage->resourceBuffer()); 439 EXPECT_FALSE(cachedImage->getContent());
438 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 440 EXPECT_FALSE(content->errorOccurred());
441 EXPECT_TRUE(content->hasImage());
439 EXPECT_EQ(2, client->imageChangedCount()); 442 EXPECT_EQ(2, client->imageChangedCount());
440 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 443 EXPECT_EQ(1, client->imageNotifyFinishedCount());
444 EXPECT_TRUE(content->getImage()->isBitmapImage());
445 EXPECT_EQ(1, content->getImage()->width());
446 EXPECT_EQ(1, content->getImage()->height());
447 ASSERT_TRUE(reloadingResource);
441 448
442 cachedImage->loader()->didReceiveResponse( 449 reloadingResource->loader()->didReceiveResponse(
443 WrappedResourceResponse(resourceResponse), nullptr); 450 WrappedResourceResponse(resourceResponse), nullptr);
444 cachedImage->loader()->didReceiveData( 451 reloadingResource->loader()->didReceiveData(
445 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2), 452 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2),
446 sizeof(kJpegImage2)); 453 sizeof(kJpegImage2));
447 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), 454 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
448 sizeof(kJpegImage2)); 455 sizeof(kJpegImage2));
449 EXPECT_FALSE(cachedImage->errorOccurred()); 456 EXPECT_FALSE(content->errorOccurred());
450 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 457 ASSERT_TRUE(content->hasImage());
451 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 458 EXPECT_FALSE(content->getImage()->isNull());
459 EXPECT_EQ(3, client->imageChangedCount());
452 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); 460 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
453 EXPECT_TRUE(client->notifyFinishedCalled());
454 461
455 // The client should not have been notified of completion again. 462 // The observer should not have been notified of completion again.
456 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); 463 EXPECT_EQ(1, client->imageNotifyFinishedCount());
457 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 464 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
458 465
459 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 466 EXPECT_TRUE(content->getImage()->isBitmapImage());
460 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 467 EXPECT_EQ(50, content->getImage()->width());
461 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 468 EXPECT_EQ(50, content->getImage()->height());
462 } 469 }
463 470
464 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { 471 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
465 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 472 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
466 ScopedRegisteredURL scopedRegisteredURL(testURL); 473 ScopedRegisteredURL scopedRegisteredURL(testURL);
467 474
468 ResourceRequest request(testURL); 475 ResourceRequest request(testURL);
469 request.setLoFiState(WebURLRequest::LoFiOn); 476 request.setLoFiState(WebURLRequest::LoFiOn);
470 FetchRequest fetchRequest(request, FetchInitiatorInfo()); 477 FetchRequest fetchRequest(request, FetchInitiatorInfo());
471 ResourceFetcher* fetcher = 478 ResourceFetcher* fetcher =
472 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 479 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
473 480
474 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); 481 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher);
482 ImageResourceContent* content = cachedImage->getContent();
475 Persistent<MockImageResourceClient> client = 483 Persistent<MockImageResourceClient> client =
476 new MockImageResourceClient(cachedImage); 484 new MockImageResourceClient(cachedImage);
477 485
486 // To test that the old ImageResource is cancelled if it has no
487 // ResourceClient when reloading is started, we remove |client| as client
488 // here, but not as observer.
489 client->removeAsClient();
490
478 // Send the image response. 491 // Send the image response.
479 ResourceResponse initialResourceResponse( 492 ResourceResponse initialResourceResponse(
480 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); 493 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String());
481 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); 494 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low");
482 495
483 cachedImage->loader()->didReceiveResponse( 496 cachedImage->loader()->didReceiveResponse(
484 WrappedResourceResponse(initialResourceResponse)); 497 WrappedResourceResponse(initialResourceResponse));
485 cachedImage->loader()->didReceiveData( 498 cachedImage->loader()->didReceiveData(
486 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage), 499 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage),
487 sizeof(kJpegImage)); 500 sizeof(kJpegImage));
488 501
489 EXPECT_FALSE(cachedImage->errorOccurred()); 502 EXPECT_FALSE(content->errorOccurred());
490 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 503 ASSERT_TRUE(content->hasImage());
491 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 504 EXPECT_FALSE(content->getImage()->isNull());
492 EXPECT_EQ(1, client->imageChangedCount()); 505 EXPECT_EQ(1, client->imageChangedCount());
493 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 506 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
494 EXPECT_FALSE(client->notifyFinishedCalled()); 507 EXPECT_TRUE(content->getImage()->isBitmapImage());
495 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 508 EXPECT_EQ(1, content->getImage()->width());
496 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); 509 EXPECT_EQ(1, content->getImage()->height());
497 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height());
498 510
499 // Call reloadIfLoFiOrPlaceholder() while the image is still loading. 511 // Call reloadIfLoFiOrPlaceholder() while the image is still loading.
500 cachedImage->reloadIfLoFiOrPlaceholder(fetcher); 512 ImageResource* reloadingResource =
501 EXPECT_FALSE(cachedImage->errorOccurred()); 513 cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
502 EXPECT_FALSE(cachedImage->resourceBuffer()); 514 EXPECT_FALSE(cachedImage->getContent());
503 EXPECT_FALSE(cachedImage->getContent()->hasImage()); 515 EXPECT_FALSE(content->errorOccurred());
504 EXPECT_EQ(2, client->imageChangedCount()); 516 // ImageResourceContent is not notified of the start of LoFi reloading.
505 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); 517 EXPECT_TRUE(content->hasImage());
518 EXPECT_EQ(1, client->imageChangedCount());
519 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
520 EXPECT_EQ(1, content->getImage()->width());
521 EXPECT_EQ(1, content->getImage()->height());
506 // The client should not have been notified of completion yet, since the image 522 // The client should not have been notified of completion yet, since the image
507 // is still loading. 523 // is still loading.
508 EXPECT_FALSE(client->notifyFinishedCalled()); 524 EXPECT_EQ(0, client->imageNotifyFinishedCount());
525 ASSERT_TRUE(reloadingResource);
509 526
510 cachedImage->loader()->didReceiveResponse( 527 reloadingResource->loader()->didReceiveResponse(
511 WrappedResourceResponse(ResourceResponse( 528 WrappedResourceResponse(ResourceResponse(
512 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), 529 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())),
513 nullptr); 530 nullptr);
514 cachedImage->loader()->didReceiveData( 531 reloadingResource->loader()->didReceiveData(
515 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2), 532 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2),
516 sizeof(kJpegImage2)); 533 sizeof(kJpegImage2));
517 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), 534 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
518 sizeof(kJpegImage2)); 535 sizeof(kJpegImage2));
519 536
520 EXPECT_FALSE(cachedImage->errorOccurred()); 537 EXPECT_FALSE(content->errorOccurred());
521 ASSERT_TRUE(cachedImage->getContent()->hasImage()); 538 // ImageResourceContent is notified of reloaded response.
522 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); 539 ASSERT_TRUE(content->hasImage());
540 EXPECT_EQ(1, client->imageNotifyFinishedCount());
541 EXPECT_EQ(2, client->imageChangedCount());
542 EXPECT_FALSE(content->getImage()->isNull());
523 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); 543 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
524 // The client should have been notified of completion only after the reload 544 // The observer should have been notified of completion only after the reload
525 // completed. 545 // completed.
526 EXPECT_TRUE(client->notifyFinishedCalled());
527 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished());
528 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); 546 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished());
529 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 547 EXPECT_TRUE(content->getImage()->isBitmapImage());
530 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 548 EXPECT_EQ(50, content->getImage()->width());
531 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 549 EXPECT_EQ(50, content->getImage()->height());
550
551 // Trigger the cancel timer, ensure the load for the first ImageResource
552 // was cancelled because reloading is started using another ImageResource.
553 EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
554 blink::testing::runPendingTasks();
555 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus());
hiroshige 2016/12/05 10:12:40 Tests that the old ImageResource is cancelled afte
532 } 556 }
533 557
534 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { 558 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
535 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 559 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
536 ScopedRegisteredURL scopedRegisteredURL(testURL); 560 ScopedRegisteredURL scopedRegisteredURL(testURL);
537 561
538 ResourceFetcher* fetcher = 562 ResourceFetcher* fetcher =
539 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 563 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
540 FetchRequest request(testURL, FetchInitiatorInfo()); 564 FetchRequest request(testURL, FetchInitiatorInfo());
541 request.setAllowImagePlaceholder(); 565 request.setAllowImagePlaceholder();
542 ImageResource* image = ImageResource::fetch(request, fetcher); 566 ImageResource* image = ImageResource::fetch(request, fetcher);
567 ImageResourceContent* content = image->getContent();
568 ASSERT_TRUE(content);
569 Persistent<MockImageResourceClient> client =
570 new MockImageResourceClient(image);
571
543 EXPECT_EQ(FetchRequest::AllowPlaceholder, 572 EXPECT_EQ(FetchRequest::AllowPlaceholder,
544 request.placeholderImageRequestType()); 573 request.placeholderImageRequestType());
545 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 574 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"));
546 Persistent<MockImageResourceClient> client =
547 new MockImageResourceClient(image);
548 575
549 ResourceResponse response(testURL, "image/jpeg", 576 ResourceResponse response(testURL, "image/jpeg",
550 kJpegImageSubrangeWithDimensionsLength, nullAtom, 577 kJpegImageSubrangeWithDimensionsLength, nullAtom,
551 String()); 578 String());
552 response.setHTTPStatusCode(206); 579 response.setHTTPStatusCode(206);
553 response.setHTTPHeaderField( 580 response.setHTTPHeaderField(
554 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, 581 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength,
555 sizeof(kJpegImage))); 582 sizeof(kJpegImage)));
556 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); 583 image->loader()->didReceiveResponse(WrappedResourceResponse(response));
557 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 584 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
558 kJpegImageSubrangeWithDimensionsLength, 585 kJpegImageSubrangeWithDimensionsLength,
559 kJpegImageSubrangeWithDimensionsLength); 586 kJpegImageSubrangeWithDimensionsLength);
560 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, 587 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength,
561 kJpegImageSubrangeWithDimensionsLength); 588 kJpegImageSubrangeWithDimensionsLength);
562 589
563 EXPECT_EQ(Resource::Cached, image->getStatus()); 590 EXPECT_EQ(Resource::Cached, image->getStatus());
564 EXPECT_TRUE(image->isPlaceholder()); 591 EXPECT_TRUE(image->isPlaceholder());
565 592
566 image->reloadIfLoFiOrPlaceholder(fetcher); 593 ImageResource* reloadingResource = image->reloadIfLoFiOrPlaceholder(fetcher);
567 594
568 EXPECT_EQ(Resource::Pending, image->getStatus()); 595 EXPECT_FALSE(image->getContent());
569 EXPECT_FALSE(image->isPlaceholder()); 596 ASSERT_TRUE(reloadingResource);
570 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 597 EXPECT_EQ(Resource::Pending, content->getStatus());
571 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), 598 EXPECT_FALSE(reloadingResource->isPlaceholder());
572 static_cast<int>(image->resourceRequest().getCachePolicy())); 599 EXPECT_EQ(nullAtom,
600 reloadingResource->resourceRequest().httpHeaderField("range"));
601 EXPECT_EQ(
602 static_cast<int>(WebCachePolicy::BypassingCache),
603 static_cast<int>(reloadingResource->resourceRequest().getCachePolicy()));
573 604
574 image->loader()->cancel(); 605 reloadingResource->loader()->cancel();
575 } 606 }
576 607
577 TEST(ImageResourceTest, SVGImage) { 608 TEST(ImageResourceTest, SVGImage) {
578 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); 609 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo");
579 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); 610 ImageResource* imageResource = ImageResource::create(ResourceRequest(url));
580 Persistent<MockImageResourceClient> client = 611 Persistent<MockImageResourceClient> client =
581 new MockImageResourceClient(imageResource); 612 new MockImageResourceClient(imageResource);
582 613
583 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, 614 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage,
584 strlen(kSvgImage)); 615 strlen(kSvgImage));
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 1035
1005 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { 1036 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) {
1006 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1037 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1007 ScopedRegisteredURL scopedRegisteredURL(testURL); 1038 ScopedRegisteredURL scopedRegisteredURL(testURL);
1008 1039
1009 FetchRequest request(testURL, FetchInitiatorInfo()); 1040 FetchRequest request(testURL, FetchInitiatorInfo());
1010 request.setAllowImagePlaceholder(); 1041 request.setAllowImagePlaceholder();
1011 ImageResource* image = ImageResource::fetch( 1042 ImageResource* image = ImageResource::fetch(
1012 request, 1043 request,
1013 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 1044 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1045 ImageResourceContent* content = image->getContent();
1014 EXPECT_EQ(FetchRequest::AllowPlaceholder, 1046 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1015 request.placeholderImageRequestType()); 1047 request.placeholderImageRequestType());
1016 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 1048 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"));
1017 EXPECT_TRUE(image->isPlaceholder()); 1049 EXPECT_TRUE(image->isPlaceholder());
1018 Persistent<MockImageResourceClient> client = 1050 Persistent<MockImageResourceClient> client =
1019 new MockImageResourceClient(image); 1051 new MockImageResourceClient(image);
1020 1052
1021 const char kBadData[] = "notanimageresponse"; 1053 const char kBadData[] = "notanimageresponse";
1022 1054
1023 image->loader()->didReceiveResponse( 1055 image->loader()->didReceiveResponse(
1024 WrappedResourceResponse(ResourceResponse( 1056 WrappedResourceResponse(ResourceResponse(
1025 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); 1057 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String())));
1026 image->loader()->didReceiveData(kBadData, sizeof(kBadData), sizeof(kBadData)); 1058 image->loader()->didReceiveData(kBadData, sizeof(kBadData), sizeof(kBadData));
1027 1059
1060 ImageResource* reloadingResource = content->resourceForTest();
1061 ASSERT_TRUE(reloadingResource);
1062 EXPECT_NE(image, reloadingResource);
1063 EXPECT_FALSE(image->getContent());
1064 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
1065
1028 // The dimensions could not be extracted, so the full original image should be 1066 // The dimensions could not be extracted, so the full original image should be
1029 // loading. 1067 // loading.
1030 EXPECT_EQ(Resource::Pending, image->getStatus()); 1068 EXPECT_EQ(Resource::Pending, content->getStatus());
1031 EXPECT_FALSE(image->isPlaceholder()); 1069 EXPECT_FALSE(reloadingResource->isPlaceholder());
1032 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1070 EXPECT_EQ(nullAtom,
1033 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), 1071 reloadingResource->resourceRequest().httpHeaderField("range"));
1034 static_cast<int>(image->resourceRequest().getCachePolicy())); 1072 EXPECT_EQ(
1035 EXPECT_FALSE(client->notifyFinishedCalled()); 1073 static_cast<int>(WebCachePolicy::BypassingCache),
1074 static_cast<int>(reloadingResource->resourceRequest().getCachePolicy()));
1036 EXPECT_EQ(0, client->imageNotifyFinishedCount()); 1075 EXPECT_EQ(0, client->imageNotifyFinishedCount());
1037 1076
1038 image->loader()->didReceiveResponse( 1077 reloadingResource->loader()->didReceiveResponse(
1039 WrappedResourceResponse(ResourceResponse( 1078 WrappedResourceResponse(ResourceResponse(
1040 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); 1079 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1041 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 1080 reloadingResource->loader()->didReceiveData(
1042 sizeof(kJpegImage), sizeof(kJpegImage)); 1081 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage),
1043 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage), 1082 sizeof(kJpegImage));
1044 sizeof(kJpegImage)); 1083 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1084 sizeof(kJpegImage));
1045 1085
1046 EXPECT_EQ(Resource::Cached, image->getStatus()); 1086 EXPECT_EQ(Resource::Cached, content->getStatus());
1047 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); 1087 EXPECT_EQ(sizeof(kJpegImage), reloadingResource->encodedSize());
1048 EXPECT_FALSE(image->isPlaceholder()); 1088 EXPECT_FALSE(reloadingResource->isPlaceholder());
1049 EXPECT_LT(0, client->imageChangedCount()); 1089 EXPECT_LT(0, client->imageChangedCount());
1050 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 1090 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
1051 EXPECT_TRUE(client->notifyFinishedCalled());
1052 EXPECT_EQ(1, client->imageNotifyFinishedCount()); 1091 EXPECT_EQ(1, client->imageNotifyFinishedCount());
1053 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
1054 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); 1092 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
1055 1093
1056 ASSERT_TRUE(image->getContent()->hasImage()); 1094 ASSERT_TRUE(content->hasImage());
1057 EXPECT_EQ(1, image->getContent()->getImage()->width()); 1095 EXPECT_EQ(1, content->getImage()->width());
1058 EXPECT_EQ(1, image->getContent()->getImage()->height()); 1096 EXPECT_EQ(1, content->getImage()->height());
1059 EXPECT_TRUE(image->getContent()->getImage()->isBitmapImage()); 1097 EXPECT_TRUE(content->getImage()->isBitmapImage());
1060 } 1098 }
1061 1099
1062 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { 1100 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
1063 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1101 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1064 ScopedRegisteredURL scopedRegisteredURL(testURL); 1102 ScopedRegisteredURL scopedRegisteredURL(testURL);
1065 1103
1066 ResourceFetcher* fetcher = 1104 ResourceFetcher* fetcher =
1067 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1105 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1068 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1106 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1069 placeholderRequest.setAllowImagePlaceholder(); 1107 placeholderRequest.setAllowImagePlaceholder();
1070 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); 1108 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher);
1109 ImageResourceContent* content = image->getContent();
1071 Persistent<MockImageResourceClient> client = 1110 Persistent<MockImageResourceClient> client =
1072 new MockImageResourceClient(image); 1111 new MockImageResourceClient(image);
1073 1112
1074 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1113 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1075 ImageResource* secondImage = 1114 ImageResource* secondImage =
1076 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1115 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1077 EXPECT_EQ(image, secondImage); 1116 EXPECT_NE(image, secondImage);
1078 EXPECT_EQ(Resource::Pending, image->getStatus()); 1117 ASSERT_EQ(secondImage, content->resourceForTest());
1079 EXPECT_FALSE(image->isPlaceholder()); 1118 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
1080 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1119 EXPECT_EQ(content, secondImage->getContent());
1120 EXPECT_EQ(Resource::Pending, content->getStatus());
1121 EXPECT_FALSE(secondImage->isPlaceholder());
1122 EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range"));
1081 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), 1123 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1082 static_cast<int>(image->resourceRequest().getCachePolicy())); 1124 static_cast<int>(secondImage->resourceRequest().getCachePolicy()));
1083 EXPECT_FALSE(client->notifyFinishedCalled()); 1125 EXPECT_FALSE(client->notifyFinishedCalled());
1084 1126
1085 image->loader()->cancel(); 1127 image->loader()->cancel();
1128 secondImage->loader()->cancel();
1086 } 1129 }
1087 1130
1088 TEST(ImageResourceTest, 1131 TEST(ImageResourceTest,
1089 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { 1132 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) {
1090 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1133 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1091 ScopedRegisteredURL scopedRegisteredURL(testURL); 1134 ScopedRegisteredURL scopedRegisteredURL(testURL);
1092 1135
1093 ResourceFetcher* fetcher = 1136 ResourceFetcher* fetcher =
1094 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1137 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1095 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1138 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1096 placeholderRequest.setAllowImagePlaceholder(); 1139 placeholderRequest.setAllowImagePlaceholder();
1097 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); 1140 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher);
1141 ImageResourceContent* content = image->getContent();
1098 Persistent<MockImageResourceClient> client = 1142 Persistent<MockImageResourceClient> client =
1099 new MockImageResourceClient(image); 1143 new MockImageResourceClient(image);
1100 1144
1101 ResourceResponse response(testURL, "image/jpeg", 1145 ResourceResponse response(testURL, "image/jpeg",
1102 kJpegImageSubrangeWithDimensionsLength, nullAtom, 1146 kJpegImageSubrangeWithDimensionsLength, nullAtom,
1103 String()); 1147 String());
1104 response.setHTTPStatusCode(206); 1148 response.setHTTPStatusCode(206);
1105 response.setHTTPHeaderField( 1149 response.setHTTPHeaderField(
1106 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, 1150 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength,
1107 sizeof(kJpegImage))); 1151 sizeof(kJpegImage)));
1108 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); 1152 image->loader()->didReceiveResponse(WrappedResourceResponse(response));
1109 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 1153 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
1110 kJpegImageSubrangeWithDimensionsLength, 1154 kJpegImageSubrangeWithDimensionsLength,
1111 kJpegImageSubrangeWithDimensionsLength); 1155 kJpegImageSubrangeWithDimensionsLength);
1112 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, 1156 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength,
1113 kJpegImageSubrangeWithDimensionsLength); 1157 kJpegImageSubrangeWithDimensionsLength);
1114 1158
1115 EXPECT_EQ(Resource::Cached, image->getStatus()); 1159 EXPECT_EQ(Resource::Cached, image->getStatus());
1116 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); 1160 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize());
1117 EXPECT_TRUE(image->isPlaceholder()); 1161 EXPECT_TRUE(image->isPlaceholder());
1118 EXPECT_LT(0, client->imageChangedCount()); 1162 EXPECT_LT(0, client->imageChangedCount());
1119 EXPECT_TRUE(client->notifyFinishedCalled()); 1163 EXPECT_TRUE(client->notifyFinishedCalled());
1120 1164
1121 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); 1165 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
1122 ImageResource* secondImage = 1166 ImageResource* secondImage =
1123 ImageResource::fetch(nonPlaceholderRequest, fetcher); 1167 ImageResource::fetch(nonPlaceholderRequest, fetcher);
1124 EXPECT_EQ(image, secondImage); 1168 EXPECT_FALSE(image->getContent());
1125 EXPECT_EQ(Resource::Pending, image->getStatus()); 1169 EXPECT_NE(image, secondImage);
1126 EXPECT_FALSE(image->isPlaceholder()); 1170 EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
1127 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1171 ASSERT_EQ(secondImage, content->resourceForTest());
1172 EXPECT_EQ(content, secondImage->getContent());
1173 EXPECT_EQ(Resource::Pending, content->getStatus());
1174 EXPECT_FALSE(secondImage->isPlaceholder());
1175 EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range"));
1128 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), 1176 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1129 static_cast<int>(image->resourceRequest().getCachePolicy())); 1177 static_cast<int>(secondImage->resourceRequest().getCachePolicy()));
1130 1178
1131 image->loader()->cancel(); 1179 secondImage->loader()->cancel();
1132 } 1180 }
1133 1181
1134 TEST(ImageResourceTest, PeriodicFlushTest) { 1182 TEST(ImageResourceTest, PeriodicFlushTest) {
1135 TestingPlatformSupportWithMockScheduler platform; 1183 TestingPlatformSupportWithMockScheduler platform;
1136 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1184 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1137 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 1185 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html",
1138 "text/html"); 1186 "text/html");
1139 ResourceRequest request = ResourceRequest(testURL); 1187 ResourceRequest request = ResourceRequest(testURL);
1140 ImageResource* cachedImage = ImageResource::create(request); 1188 ImageResource* cachedImage = ImageResource::create(request);
1141 cachedImage->setStatus(Resource::Pending); 1189 cachedImage->setStatus(Resource::Pending);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 EXPECT_TRUE(client->notifyFinishedCalled()); 1260 EXPECT_TRUE(client->notifyFinishedCalled());
1213 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); 1261 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
1214 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); 1262 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width());
1215 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); 1263 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height());
1216 1264
1217 WTF::setTimeFunctionsForTesting(nullptr); 1265 WTF::setTimeFunctionsForTesting(nullptr);
1218 } 1266 }
1219 1267
1220 } // namespace 1268 } // namespace
1221 } // namespace blink 1269 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/ImageResourceInfo.h ('k') | third_party/WebKit/Source/core/fetch/MockResourceClients.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698