| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 #include "public/platform/WebURL.h" | 51 #include "public/platform/WebURL.h" |
| 52 #include "public/platform/WebURLLoaderMockFactory.h" | 52 #include "public/platform/WebURLLoaderMockFactory.h" |
| 53 #include "public/platform/WebURLResponse.h" | 53 #include "public/platform/WebURLResponse.h" |
| 54 #include "testing/gtest/include/gtest/gtest.h" | 54 #include "testing/gtest/include/gtest/gtest.h" |
| 55 #include "wtf/PtrUtil.h" | 55 #include "wtf/PtrUtil.h" |
| 56 #include "wtf/text/Base64.h" | 56 #include "wtf/text/Base64.h" |
| 57 #include <memory> | 57 #include <memory> |
| 58 | 58 |
| 59 namespace blink { | 59 namespace blink { |
| 60 | 60 |
| 61 // TODO(hiroshige): Cleanup variable names for ImageResource. |
| 62 // TODO(hiroshige): Cleanup usage of ImageResource::fetch(), create(), and |
| 63 // URLLoaderMock. |
| 64 |
| 61 namespace { | 65 namespace { |
| 62 | 66 |
| 63 // An image of size 1x1. | 67 // An image of size 1x1. |
| 64 const unsigned char kJpegImage[] = { | 68 const unsigned char kJpegImage[] = { |
| 65 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, | 69 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, |
| 66 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, | 70 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x13, |
| 67 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, | 71 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, |
| 68 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x05, 0x03, | 72 0x20, 0x47, 0x49, 0x4d, 0x50, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x05, 0x03, |
| 69 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, | 73 0x04, 0x04, 0x04, 0x03, 0x05, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, |
| 70 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, | 74 0x07, 0x0c, 0x08, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x0b, 0x0b, 0x09, 0x0c, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 315 |
| 312 fetcher->startLoad(cachedImage); | 316 fetcher->startLoad(cachedImage); |
| 313 memoryCache()->add(cachedImage); | 317 memoryCache()->add(cachedImage); |
| 314 | 318 |
| 315 Persistent<MockImageResourceClient> client = | 319 Persistent<MockImageResourceClient> client = |
| 316 new MockImageResourceClient(cachedImage); | 320 new MockImageResourceClient(cachedImage); |
| 317 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); | 321 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 318 | 322 |
| 319 // The load should still be alive, but a timer should be started to cancel the | 323 // The load should still be alive, but a timer should be started to cancel the |
| 320 // load inside removeClient(). | 324 // load inside removeClient(). |
| 321 client->removeAsClient(); | 325 client->removeAsClientAndObserver(); |
| 322 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); | 326 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 323 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); | 327 EXPECT_TRUE(memoryCache()->resourceForURL(testURL)); |
| 324 | 328 |
| 325 // Trigger the cancel timer, ensure the load was cancelled and the resource | 329 // Trigger the cancel timer, ensure the load was cancelled and the resource |
| 326 // was evicted from the cache. | 330 // was evicted from the cache. |
| 327 blink::testing::runPendingTasks(); | 331 blink::testing::runPendingTasks(); |
| 328 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); | 332 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); |
| 329 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); | 333 EXPECT_FALSE(memoryCache()->resourceForURL(testURL)); |
| 330 } | 334 } |
| 331 | 335 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 358 | 362 |
| 359 // The prune comes when the ImageResource still has clients. The image should | 363 // The prune comes when the ImageResource still has clients. The image should |
| 360 // not be deleted. | 364 // not be deleted. |
| 361 cachedImage->prune(); | 365 cachedImage->prune(); |
| 362 EXPECT_TRUE(cachedImage->isAlive()); | 366 EXPECT_TRUE(cachedImage->isAlive()); |
| 363 ASSERT_TRUE(cachedImage->getContent()->hasImage()); | 367 ASSERT_TRUE(cachedImage->getContent()->hasImage()); |
| 364 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); | 368 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); |
| 365 | 369 |
| 366 // The ImageResource no longer has clients. The decoded image data should be | 370 // The ImageResource no longer has clients. The decoded image data should be |
| 367 // deleted by prune. | 371 // deleted by prune. |
| 368 client->removeAsClient(); | 372 client->removeAsClientAndObserver(); |
| 369 cachedImage->prune(); | 373 cachedImage->prune(); |
| 370 EXPECT_FALSE(cachedImage->isAlive()); | 374 EXPECT_FALSE(cachedImage->isAlive()); |
| 371 EXPECT_TRUE(cachedImage->getContent()->hasImage()); | 375 EXPECT_TRUE(cachedImage->getContent()->hasImage()); |
| 372 // TODO(hajimehoshi): Should check cachedImage doesn't have decoded image | 376 // TODO(hajimehoshi): Should check cachedImage doesn't have decoded image |
| 373 // data. | 377 // data. |
| 374 } | 378 } |
| 375 | 379 |
| 376 TEST(ImageResourceTest, UpdateBitmapImages) { | 380 TEST(ImageResourceTest, UpdateBitmapImages) { |
| 377 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); | 381 ImageResource* cachedImage = ImageResource::create(ResourceRequest()); |
| 378 cachedImage->setStatus(Resource::Pending); | 382 cachedImage->setStatus(Resource::Pending); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 395 EXPECT_TRUE(client->notifyFinishedCalled()); | 399 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 396 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 400 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); |
| 397 } | 401 } |
| 398 | 402 |
| 399 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { | 403 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) { |
| 400 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 404 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 401 ScopedRegisteredURL scopedRegisteredURL(testURL); | 405 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 402 ResourceRequest request = ResourceRequest(testURL); | 406 ResourceRequest request = ResourceRequest(testURL); |
| 403 request.setLoFiState(WebURLRequest::LoFiOn); | 407 request.setLoFiState(WebURLRequest::LoFiOn); |
| 404 ImageResource* cachedImage = ImageResource::create(request); | 408 ImageResource* cachedImage = ImageResource::create(request); |
| 409 ImageResourceContent* content = cachedImage->getContent(); |
| 405 cachedImage->setStatus(Resource::Pending); | 410 cachedImage->setStatus(Resource::Pending); |
| 406 | 411 |
| 407 Persistent<MockImageResourceClient> client = | 412 Persistent<MockImageResourceClient> client = |
| 408 new MockImageResourceClient(cachedImage); | 413 new MockImageResourceClient(cachedImage); |
| 409 ResourceFetcher* fetcher = | 414 ResourceFetcher* fetcher = |
| 410 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 415 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 411 | 416 |
| 412 // Send the image response. | 417 // Send the image response. |
| 413 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), | 418 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 414 nullAtom, String()); | 419 nullAtom, String()); |
| 415 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", | 420 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", |
| 416 "empty-image"); | 421 "empty-image"); |
| 417 | 422 |
| 418 cachedImage->responseReceived(resourceResponse, nullptr); | 423 cachedImage->responseReceived(resourceResponse, nullptr); |
| 419 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), | 424 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage), |
| 420 sizeof(kJpegImage)); | 425 sizeof(kJpegImage)); |
| 421 cachedImage->finish(); | 426 cachedImage->finish(); |
| 422 EXPECT_FALSE(cachedImage->errorOccurred()); | 427 EXPECT_FALSE(content->errorOccurred()); |
| 423 ASSERT_TRUE(cachedImage->getContent()->hasImage()); | 428 ASSERT_TRUE(content->hasImage()); |
| 424 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); | 429 EXPECT_FALSE(content->getImage()->isNull()); |
| 425 EXPECT_EQ(2, client->imageChangedCount()); | 430 EXPECT_EQ(2, client->imageChangedCount()); |
| 426 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 431 EXPECT_EQ(1, client->imageNotifyFinishedCount()); |
| 427 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 432 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 428 // The client should have been notified that the image load completed. | 433 // The client should have been notified that the image load completed. |
| 429 EXPECT_TRUE(client->notifyFinishedCalled()); | 434 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 430 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | 435 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); |
| 431 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | 436 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 432 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 437 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 433 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); | 438 EXPECT_EQ(1, content->getImage()->width()); |
| 434 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); | 439 EXPECT_EQ(1, content->getImage()->height()); |
| 435 | 440 |
| 436 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. | 441 // Call reloadIfLoFiOrPlaceholderImage() after the image has finished loading. |
| 437 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); | 442 ImageResource* reloadingResource = |
| 438 EXPECT_FALSE(cachedImage->errorOccurred()); | 443 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, |
| 439 EXPECT_FALSE(cachedImage->resourceBuffer()); | 444 Resource::kReloadAlways); |
| 440 EXPECT_FALSE(cachedImage->getContent()->hasImage()); | 445 ASSERT_TRUE(reloadingResource); |
| 446 EXPECT_NE(cachedImage, reloadingResource); |
| 447 EXPECT_EQ(content, reloadingResource->getContent()); |
| 448 EXPECT_FALSE(cachedImage->getContent()); |
| 449 EXPECT_EQ(reloadingResource, fetcher->cachedResource(testURL)); |
| 450 EXPECT_EQ(reloadingResource, memoryCache()->resourceForURL(testURL)); |
| 451 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 452 |
| 453 EXPECT_FALSE(content->errorOccurred()); |
| 454 EXPECT_TRUE(content->hasImage()); |
| 455 EXPECT_EQ(2, client->imageChangedCount()); |
| 456 EXPECT_EQ(1, client->imageNotifyFinishedCount()); |
| 457 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 458 EXPECT_EQ(1, content->getImage()->width()); |
| 459 EXPECT_EQ(1, content->getImage()->height()); |
| 460 |
| 461 reloadingResource->loader()->didReceiveResponse( |
| 462 WrappedResourceResponse(resourceResponse), nullptr); |
| 463 reloadingResource->loader()->didReceiveData( |
| 464 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); |
| 465 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), |
| 466 sizeof(kJpegImage2)); |
| 467 EXPECT_FALSE(content->errorOccurred()); |
| 468 ASSERT_TRUE(content->hasImage()); |
| 469 EXPECT_FALSE(content->getImage()->isNull()); |
| 441 EXPECT_EQ(3, client->imageChangedCount()); | 470 EXPECT_EQ(3, client->imageChangedCount()); |
| 471 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
| 472 |
| 473 // The observer should not have been notified of completion again. |
| 442 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 474 EXPECT_EQ(1, client->imageNotifyFinishedCount()); |
| 475 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 476 |
| 477 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 478 EXPECT_EQ(50, content->getImage()->width()); |
| 479 EXPECT_EQ(50, content->getImage()->height()); |
| 480 } |
| 481 |
| 482 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) { |
| 483 ResourceFetcher* fetcher = |
| 484 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 485 |
| 486 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 487 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 488 |
| 489 ResourceRequest request = ResourceRequest(testURL); |
| 490 request.setLoFiState(WebURLRequest::LoFiOn); |
| 491 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
| 492 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); |
| 493 ImageResourceContent* content = cachedImage->getContent(); |
| 494 |
| 495 Persistent<MockImageResourceClient> client = |
| 496 new MockImageResourceClient(cachedImage); |
| 497 |
| 498 // Send the image response. |
| 499 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), |
| 500 nullAtom, String()); |
| 501 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", |
| 502 "empty-image"); |
| 443 | 503 |
| 444 cachedImage->loader()->didReceiveResponse( | 504 cachedImage->loader()->didReceiveResponse( |
| 445 WrappedResourceResponse(resourceResponse), nullptr); | 505 WrappedResourceResponse(resourceResponse)); |
| 446 cachedImage->loader()->didReceiveData( | 506 cachedImage->loader()->didReceiveData( |
| 447 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | 507 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 448 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | 508 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 449 sizeof(kJpegImage2)); | 509 sizeof(kJpegImage)); |
| 450 EXPECT_FALSE(cachedImage->errorOccurred()); | |
| 451 ASSERT_TRUE(cachedImage->getContent()->hasImage()); | |
| 452 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); | |
| 453 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); | |
| 454 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 455 | 510 |
| 456 // The client should not have been notified of completion again. | 511 EXPECT_EQ(cachedImage, fetcher->cachedResource(testURL)); |
| 457 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | 512 fetcher->reloadLoFiImages(); |
| 458 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | 513 const ImageResource* reloadingResource = content->resourceForTest(); |
| 459 | 514 |
| 460 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 515 ASSERT_TRUE(reloadingResource); |
| 461 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); | 516 EXPECT_NE(cachedImage, reloadingResource); |
| 462 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); | 517 EXPECT_EQ(content, reloadingResource->getContent()); |
| 518 EXPECT_FALSE(cachedImage->getContent()); |
| 519 EXPECT_EQ(reloadingResource, fetcher->cachedResource(testURL)); |
| 520 EXPECT_EQ(reloadingResource, memoryCache()->resourceForURL(testURL)); |
| 521 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 522 |
| 523 reloadingResource->loader()->cancel(); |
| 463 } | 524 } |
| 464 | 525 |
| 465 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { | 526 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
| 466 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 527 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 467 ScopedRegisteredURL scopedRegisteredURL(testURL); | 528 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 468 | 529 |
| 469 ResourceRequest request(testURL); | 530 ResourceRequest request(testURL); |
| 470 request.setLoFiState(WebURLRequest::LoFiOn); | 531 request.setLoFiState(WebURLRequest::LoFiOn); |
| 471 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | 532 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
| 472 ResourceFetcher* fetcher = | 533 ResourceFetcher* fetcher = |
| 473 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 534 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 474 | 535 |
| 475 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); | 536 ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher); |
| 537 ImageResourceContent* content = cachedImage->getContent(); |
| 476 Persistent<MockImageResourceClient> client = | 538 Persistent<MockImageResourceClient> client = |
| 477 new MockImageResourceClient(cachedImage); | 539 new MockImageResourceClient(cachedImage); |
| 478 | 540 |
| 541 // To test that the old ImageResource is cancelled if it has no |
| 542 // ResourceClient when reloading is started, we remove |client| as client |
| 543 // here, but not as observer. |
| 544 client->removeAsClient(); |
| 545 |
| 479 // Send the image response. | 546 // Send the image response. |
| 480 ResourceResponse initialResourceResponse( | 547 ResourceResponse initialResourceResponse( |
| 481 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); | 548 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()); |
| 482 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 549 initialResourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 483 | 550 |
| 484 cachedImage->loader()->didReceiveResponse( | 551 cachedImage->loader()->didReceiveResponse( |
| 485 WrappedResourceResponse(initialResourceResponse)); | 552 WrappedResourceResponse(initialResourceResponse)); |
| 486 cachedImage->loader()->didReceiveData( | 553 cachedImage->loader()->didReceiveData( |
| 487 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | 554 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 488 | 555 |
| 489 EXPECT_FALSE(cachedImage->errorOccurred()); | 556 EXPECT_FALSE(content->errorOccurred()); |
| 490 ASSERT_TRUE(cachedImage->getContent()->hasImage()); | 557 ASSERT_TRUE(content->hasImage()); |
| 491 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); | 558 EXPECT_FALSE(content->getImage()->isNull()); |
| 492 EXPECT_EQ(1, client->imageChangedCount()); | 559 EXPECT_EQ(1, client->imageChangedCount()); |
| 493 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 560 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 494 EXPECT_FALSE(client->notifyFinishedCalled()); | 561 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 495 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 562 EXPECT_EQ(1, content->getImage()->width()); |
| 496 EXPECT_EQ(1, cachedImage->getContent()->getImage()->width()); | 563 EXPECT_EQ(1, content->getImage()->height()); |
| 497 EXPECT_EQ(1, cachedImage->getContent()->getImage()->height()); | |
| 498 | 564 |
| 499 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. | 565 // Call reloadIfLoFiOrPlaceholderImage() while the image is still loading. |
| 500 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); | 566 ImageResource* reloadingResource = |
| 501 EXPECT_FALSE(cachedImage->errorOccurred()); | 567 cachedImage->reloadIfLoFiOrPlaceholderImage(fetcher, |
| 502 EXPECT_FALSE(cachedImage->resourceBuffer()); | 568 Resource::kReloadAlways); |
| 503 EXPECT_FALSE(cachedImage->getContent()->hasImage()); | 569 |
| 504 EXPECT_EQ(2, client->imageChangedCount()); | 570 ASSERT_TRUE(reloadingResource); |
| 505 EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged()); | 571 EXPECT_NE(cachedImage, reloadingResource); |
| 572 EXPECT_EQ(content, reloadingResource->getContent()); |
| 573 EXPECT_FALSE(cachedImage->getContent()); |
| 574 EXPECT_EQ(reloadingResource, fetcher->cachedResource(testURL)); |
| 575 EXPECT_EQ(reloadingResource, memoryCache()->resourceForURL(testURL)); |
| 576 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 577 |
| 578 EXPECT_FALSE(content->errorOccurred()); |
| 579 // ImageResourceContent is not notified of the start of LoFi reloading. |
| 580 EXPECT_TRUE(content->hasImage()); |
| 581 EXPECT_EQ(1, client->imageChangedCount()); |
| 582 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 583 EXPECT_EQ(1, content->getImage()->width()); |
| 584 EXPECT_EQ(1, content->getImage()->height()); |
| 506 // The client should not have been notified of completion yet, since the image | 585 // The client should not have been notified of completion yet, since the image |
| 507 // is still loading. | 586 // is still loading. |
| 508 EXPECT_FALSE(client->notifyFinishedCalled()); | 587 EXPECT_EQ(0, client->imageNotifyFinishedCount()); |
| 509 | 588 |
| 510 cachedImage->loader()->didReceiveResponse( | 589 reloadingResource->loader()->didReceiveResponse( |
| 511 WrappedResourceResponse(ResourceResponse( | 590 WrappedResourceResponse(ResourceResponse( |
| 512 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), | 591 testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())), |
| 513 nullptr); | 592 nullptr); |
| 514 cachedImage->loader()->didReceiveData( | 593 reloadingResource->loader()->didReceiveData( |
| 515 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | 594 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); |
| 516 cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | 595 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), |
| 517 sizeof(kJpegImage2)); | 596 sizeof(kJpegImage2)); |
| 518 | 597 |
| 519 EXPECT_FALSE(cachedImage->errorOccurred()); | 598 EXPECT_FALSE(content->errorOccurred()); |
| 520 ASSERT_TRUE(cachedImage->getContent()->hasImage()); | 599 // ImageResourceContent is notified of reloaded response. |
| 521 EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull()); | 600 ASSERT_TRUE(content->hasImage()); |
| 601 EXPECT_EQ(1, client->imageNotifyFinishedCount()); |
| 602 EXPECT_EQ(2, client->imageChangedCount()); |
| 603 EXPECT_FALSE(content->getImage()->isNull()); |
| 522 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); | 604 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged()); |
| 523 // The client should have been notified of completion only after the reload | 605 // The observer should have been notified of completion only after the reload |
| 524 // completed. | 606 // completed. |
| 525 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 526 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished()); | |
| 527 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); | 607 EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished()); |
| 528 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 608 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 529 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); | 609 EXPECT_EQ(50, content->getImage()->width()); |
| 530 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); | 610 EXPECT_EQ(50, content->getImage()->height()); |
| 611 |
| 612 // Trigger the cancel timer, ensure the load for the first ImageResource |
| 613 // was cancelled because reloading is started using another ImageResource. |
| 614 EXPECT_EQ(Resource::Pending, cachedImage->getStatus()); |
| 615 blink::testing::runPendingTasks(); |
| 616 EXPECT_EQ(Resource::LoadError, cachedImage->getStatus()); |
| 531 } | 617 } |
| 532 | 618 |
| 533 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { | 619 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) { |
| 534 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 620 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 535 ScopedRegisteredURL scopedRegisteredURL(testURL); | 621 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 536 | 622 |
| 537 ResourceFetcher* fetcher = | 623 ResourceFetcher* fetcher = |
| 538 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 624 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 539 FetchRequest request(testURL, FetchInitiatorInfo()); | 625 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 540 request.setAllowImagePlaceholder(); | 626 request.setAllowImagePlaceholder(); |
| 541 ImageResource* image = ImageResource::fetch(request, fetcher); | 627 ImageResource* image = ImageResource::fetch(request, fetcher); |
| 628 ImageResourceContent* content = image->getContent(); |
| 629 ASSERT_TRUE(content); |
| 630 Persistent<MockImageResourceClient> client = |
| 631 new MockImageResourceClient(image); |
| 632 |
| 542 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 633 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 543 request.placeholderImageRequestType()); | 634 request.placeholderImageRequestType()); |
| 544 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); | 635 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 545 Persistent<MockImageResourceClient> client = | |
| 546 new MockImageResourceClient(image); | |
| 547 | 636 |
| 548 ResourceResponse response(testURL, "image/jpeg", | 637 ResourceResponse response(testURL, "image/jpeg", |
| 549 kJpegImageSubrangeWithDimensionsLength, nullAtom, | 638 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 550 String()); | 639 String()); |
| 551 response.setHTTPStatusCode(206); | 640 response.setHTTPStatusCode(206); |
| 552 response.setHTTPHeaderField( | 641 response.setHTTPHeaderField( |
| 553 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, | 642 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 554 sizeof(kJpegImage))); | 643 sizeof(kJpegImage))); |
| 555 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); | 644 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); |
| 556 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), | 645 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), |
| 557 kJpegImageSubrangeWithDimensionsLength); | 646 kJpegImageSubrangeWithDimensionsLength); |
| 558 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, | 647 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, |
| 559 kJpegImageSubrangeWithDimensionsLength); | 648 kJpegImageSubrangeWithDimensionsLength); |
| 560 | 649 |
| 561 EXPECT_EQ(Resource::Cached, image->getStatus()); | 650 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 562 EXPECT_TRUE(image->isPlaceholder()); | 651 EXPECT_TRUE(image->isPlaceholder()); |
| 563 | 652 |
| 564 image->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); | 653 ImageResource* reloadingResource = |
| 654 image->reloadIfLoFiOrPlaceholderImage(fetcher, Resource::kReloadAlways); |
| 565 | 655 |
| 566 EXPECT_EQ(Resource::Pending, image->getStatus()); | 656 ASSERT_TRUE(reloadingResource); |
| 567 EXPECT_FALSE(image->isPlaceholder()); | 657 EXPECT_NE(image, reloadingResource); |
| 568 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); | 658 EXPECT_EQ(content, reloadingResource->getContent()); |
| 569 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), | 659 EXPECT_FALSE(image->getContent()); |
| 570 static_cast<int>(image->resourceRequest().getCachePolicy())); | 660 EXPECT_EQ(reloadingResource, fetcher->cachedResource(testURL)); |
| 661 EXPECT_EQ(reloadingResource, memoryCache()->resourceForURL(testURL)); |
| 662 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 571 | 663 |
| 572 image->loader()->cancel(); | 664 EXPECT_EQ(Resource::Pending, content->getStatus()); |
| 665 EXPECT_EQ(nullAtom, |
| 666 reloadingResource->resourceRequest().httpHeaderField("range")); |
| 667 EXPECT_EQ( |
| 668 static_cast<int>(WebCachePolicy::BypassingCache), |
| 669 static_cast<int>(reloadingResource->resourceRequest().getCachePolicy())); |
| 670 |
| 671 reloadingResource->loader()->cancel(); |
| 573 } | 672 } |
| 574 | 673 |
| 575 TEST(ImageResourceTest, SVGImage) { | 674 TEST(ImageResourceTest, SVGImage) { |
| 576 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); | 675 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo"); |
| 577 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); | 676 ImageResource* imageResource = ImageResource::create(ResourceRequest(url)); |
| 578 Persistent<MockImageResourceClient> client = | 677 Persistent<MockImageResourceClient> client = |
| 579 new MockImageResourceClient(imageResource); | 678 new MockImageResourceClient(imageResource); |
| 580 | 679 |
| 581 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, | 680 receiveResponse(imageResource, url, "image/svg+xml", kSvgImage, |
| 582 strlen(kSvgImage)); | 681 strlen(kSvgImage)); |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 EXPECT_FALSE(image->getContent()->getImage()->isBitmapImage()); | 1096 EXPECT_FALSE(image->getContent()->getImage()->isBitmapImage()); |
| 998 EXPECT_FALSE(image->getContent()->getImage()->isSVGImage()); | 1097 EXPECT_FALSE(image->getContent()->getImage()->isSVGImage()); |
| 999 } | 1098 } |
| 1000 | 1099 |
| 1001 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { | 1100 TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) { |
| 1002 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1101 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1003 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1102 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1004 | 1103 |
| 1005 FetchRequest request(testURL, FetchInitiatorInfo()); | 1104 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1006 request.setAllowImagePlaceholder(); | 1105 request.setAllowImagePlaceholder(); |
| 1007 ImageResource* image = ImageResource::fetch( | 1106 ResourceFetcher* fetcher = |
| 1008 request, | 1107 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1009 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | 1108 ImageResource* image = ImageResource::fetch(request, fetcher); |
| 1109 ImageResourceContent* content = image->getContent(); |
| 1010 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 1110 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1011 request.placeholderImageRequestType()); | 1111 request.placeholderImageRequestType()); |
| 1012 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); | 1112 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); |
| 1013 EXPECT_TRUE(image->isPlaceholder()); | 1113 EXPECT_TRUE(image->isPlaceholder()); |
| 1014 Persistent<MockImageResourceClient> client = | 1114 Persistent<MockImageResourceClient> client = |
| 1015 new MockImageResourceClient(image); | 1115 new MockImageResourceClient(image); |
| 1016 | 1116 |
| 1017 const char kBadData[] = "notanimageresponse"; | 1117 const char kBadData[] = "notanimageresponse"; |
| 1018 | 1118 |
| 1019 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( | 1119 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( |
| 1020 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); | 1120 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); |
| 1021 image->loader()->didReceiveData(kBadData, sizeof(kBadData)); | 1121 image->loader()->didReceiveData(kBadData, sizeof(kBadData)); |
| 1022 | 1122 |
| 1123 const ImageResource* reloadingResource = content->resourceForTest(); |
| 1124 |
| 1125 ASSERT_TRUE(reloadingResource); |
| 1126 EXPECT_NE(image, reloadingResource); |
| 1127 EXPECT_EQ(content, reloadingResource->getContent()); |
| 1128 EXPECT_FALSE(image->getContent()); |
| 1129 EXPECT_EQ(reloadingResource, fetcher->cachedResource(testURL)); |
| 1130 EXPECT_EQ(reloadingResource, memoryCache()->resourceForURL(testURL)); |
| 1131 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 1132 |
| 1023 // The dimensions could not be extracted, so the full original image should be | 1133 // The dimensions could not be extracted, so the full original image should be |
| 1024 // loading. | 1134 // loading. |
| 1025 EXPECT_EQ(Resource::Pending, image->getStatus()); | 1135 EXPECT_EQ(Resource::Pending, content->getStatus()); |
| 1026 EXPECT_FALSE(image->isPlaceholder()); | 1136 EXPECT_EQ(nullAtom, |
| 1027 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); | 1137 reloadingResource->resourceRequest().httpHeaderField("range")); |
| 1028 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), | 1138 EXPECT_EQ( |
| 1029 static_cast<int>(image->resourceRequest().getCachePolicy())); | 1139 static_cast<int>(WebCachePolicy::BypassingCache), |
| 1030 EXPECT_FALSE(client->notifyFinishedCalled()); | 1140 static_cast<int>(reloadingResource->resourceRequest().getCachePolicy())); |
| 1031 EXPECT_EQ(0, client->imageNotifyFinishedCount()); | 1141 EXPECT_EQ(0, client->imageNotifyFinishedCount()); |
| 1032 | 1142 |
| 1033 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse( | 1143 reloadingResource->loader()->didReceiveResponse( |
| 1034 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); | 1144 WrappedResourceResponse(ResourceResponse( |
| 1035 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), | 1145 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); |
| 1036 sizeof(kJpegImage)); | 1146 reloadingResource->loader()->didReceiveData( |
| 1037 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | 1147 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 1038 sizeof(kJpegImage)); | 1148 reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 1149 sizeof(kJpegImage)); |
| 1039 | 1150 |
| 1040 EXPECT_EQ(Resource::Cached, image->getStatus()); | 1151 EXPECT_EQ(Resource::Cached, content->getStatus()); |
| 1041 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); | 1152 EXPECT_EQ(sizeof(kJpegImage), reloadingResource->encodedSize()); |
| 1042 EXPECT_FALSE(image->isPlaceholder()); | 1153 EXPECT_FALSE(reloadingResource->isPlaceholder()); |
| 1043 EXPECT_LT(0, client->imageChangedCount()); | 1154 EXPECT_LT(0, client->imageChangedCount()); |
| 1044 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); | 1155 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); |
| 1045 EXPECT_TRUE(client->notifyFinishedCalled()); | |
| 1046 EXPECT_EQ(1, client->imageNotifyFinishedCount()); | 1156 EXPECT_EQ(1, client->imageNotifyFinishedCount()); |
| 1047 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished()); | |
| 1048 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); | 1157 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished()); |
| 1049 | 1158 |
| 1050 ASSERT_TRUE(image->getContent()->hasImage()); | 1159 ASSERT_TRUE(content->hasImage()); |
| 1051 EXPECT_EQ(1, image->getContent()->getImage()->width()); | 1160 EXPECT_EQ(1, content->getImage()->width()); |
| 1052 EXPECT_EQ(1, image->getContent()->getImage()->height()); | 1161 EXPECT_EQ(1, content->getImage()->height()); |
| 1053 EXPECT_TRUE(image->getContent()->getImage()->isBitmapImage()); | 1162 EXPECT_TRUE(content->getImage()->isBitmapImage()); |
| 1054 } | 1163 } |
| 1055 | 1164 |
| 1056 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { | 1165 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
| 1057 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1166 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1058 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1167 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1059 | 1168 |
| 1060 ResourceFetcher* fetcher = | 1169 ResourceFetcher* fetcher = |
| 1061 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 1170 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1062 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1171 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1063 placeholderRequest.setAllowImagePlaceholder(); | 1172 placeholderRequest.setAllowImagePlaceholder(); |
| 1064 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); | 1173 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1174 ImageResourceContent* content = image->getContent(); |
| 1065 Persistent<MockImageResourceClient> client = | 1175 Persistent<MockImageResourceClient> client = |
| 1066 new MockImageResourceClient(image); | 1176 new MockImageResourceClient(image); |
| 1067 | 1177 |
| 1068 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1178 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1069 ImageResource* secondImage = | 1179 ImageResource* secondImage = |
| 1070 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1180 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1071 EXPECT_EQ(image, secondImage); | 1181 EXPECT_NE(image, secondImage); |
| 1072 EXPECT_EQ(Resource::Pending, image->getStatus()); | 1182 ASSERT_EQ(secondImage, content->resourceForTest()); |
| 1073 EXPECT_FALSE(image->isPlaceholder()); | 1183 EXPECT_EQ(secondImage, memoryCache()->resourceForURL(testURL)); |
| 1074 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); | 1184 EXPECT_EQ(content, secondImage->getContent()); |
| 1185 EXPECT_EQ(Resource::Pending, content->getStatus()); |
| 1186 EXPECT_FALSE(secondImage->isPlaceholder()); |
| 1187 EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range")); |
| 1075 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1188 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1076 static_cast<int>(image->resourceRequest().getCachePolicy())); | 1189 static_cast<int>(secondImage->resourceRequest().getCachePolicy())); |
| 1077 EXPECT_FALSE(client->notifyFinishedCalled()); | 1190 EXPECT_FALSE(client->notifyFinishedCalled()); |
| 1078 | 1191 |
| 1079 image->loader()->cancel(); | 1192 image->loader()->cancel(); |
| 1193 secondImage->loader()->cancel(); |
| 1080 } | 1194 } |
| 1081 | 1195 |
| 1082 TEST(ImageResourceTest, | 1196 TEST(ImageResourceTest, |
| 1083 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { | 1197 FetchAllowPlaceholderThenDisallowPlaceholderAfterLoaded) { |
| 1084 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1198 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1085 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1199 ScopedRegisteredURL scopedRegisteredURL(testURL); |
| 1086 | 1200 |
| 1087 ResourceFetcher* fetcher = | 1201 ResourceFetcher* fetcher = |
| 1088 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 1202 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 1089 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1203 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
| 1090 placeholderRequest.setAllowImagePlaceholder(); | 1204 placeholderRequest.setAllowImagePlaceholder(); |
| 1091 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); | 1205 ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher); |
| 1206 ImageResourceContent* content = image->getContent(); |
| 1092 Persistent<MockImageResourceClient> client = | 1207 Persistent<MockImageResourceClient> client = |
| 1093 new MockImageResourceClient(image); | 1208 new MockImageResourceClient(image); |
| 1094 | 1209 |
| 1095 ResourceResponse response(testURL, "image/jpeg", | 1210 ResourceResponse response(testURL, "image/jpeg", |
| 1096 kJpegImageSubrangeWithDimensionsLength, nullAtom, | 1211 kJpegImageSubrangeWithDimensionsLength, nullAtom, |
| 1097 String()); | 1212 String()); |
| 1098 response.setHTTPStatusCode(206); | 1213 response.setHTTPStatusCode(206); |
| 1099 response.setHTTPHeaderField( | 1214 response.setHTTPHeaderField( |
| 1100 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, | 1215 "content-range", buildContentRange(kJpegImageSubrangeWithDimensionsLength, |
| 1101 sizeof(kJpegImage))); | 1216 sizeof(kJpegImage))); |
| 1102 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); | 1217 image->loader()->didReceiveResponse(WrappedResourceResponse(response)); |
| 1103 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), | 1218 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), |
| 1104 kJpegImageSubrangeWithDimensionsLength); | 1219 kJpegImageSubrangeWithDimensionsLength); |
| 1105 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, | 1220 image->loader()->didFinishLoading(0.0, kJpegImageSubrangeWithDimensionsLength, |
| 1106 kJpegImageSubrangeWithDimensionsLength); | 1221 kJpegImageSubrangeWithDimensionsLength); |
| 1107 | 1222 |
| 1108 EXPECT_EQ(Resource::Cached, image->getStatus()); | 1223 EXPECT_EQ(Resource::Cached, image->getStatus()); |
| 1109 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); | 1224 EXPECT_EQ(kJpegImageSubrangeWithDimensionsLength, image->encodedSize()); |
| 1110 EXPECT_TRUE(image->isPlaceholder()); | 1225 EXPECT_TRUE(image->isPlaceholder()); |
| 1111 EXPECT_LT(0, client->imageChangedCount()); | 1226 EXPECT_LT(0, client->imageChangedCount()); |
| 1112 EXPECT_TRUE(client->notifyFinishedCalled()); | 1227 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1113 | 1228 |
| 1114 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); | 1229 FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo()); |
| 1115 ImageResource* secondImage = | 1230 ImageResource* secondImage = |
| 1116 ImageResource::fetch(nonPlaceholderRequest, fetcher); | 1231 ImageResource::fetch(nonPlaceholderRequest, fetcher); |
| 1117 EXPECT_EQ(image, secondImage); | 1232 EXPECT_FALSE(image->getContent()); |
| 1118 EXPECT_EQ(Resource::Pending, image->getStatus()); | 1233 EXPECT_NE(image, secondImage); |
| 1119 EXPECT_FALSE(image->isPlaceholder()); | 1234 EXPECT_EQ(secondImage, memoryCache()->resourceForURL(testURL)); |
| 1120 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); | 1235 ASSERT_EQ(secondImage, content->resourceForTest()); |
| 1236 EXPECT_EQ(content, secondImage->getContent()); |
| 1237 EXPECT_EQ(Resource::Pending, content->getStatus()); |
| 1238 EXPECT_FALSE(secondImage->isPlaceholder()); |
| 1239 EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range")); |
| 1121 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1240 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1122 static_cast<int>(image->resourceRequest().getCachePolicy())); | 1241 static_cast<int>(secondImage->resourceRequest().getCachePolicy())); |
| 1123 | 1242 |
| 1124 image->loader()->cancel(); | 1243 secondImage->loader()->cancel(); |
| 1125 } | 1244 } |
| 1126 | 1245 |
| 1127 TEST(ImageResourceTest, PeriodicFlushTest) { | 1246 TEST(ImageResourceTest, PeriodicFlushTest) { |
| 1128 TestingPlatformSupportWithMockScheduler platform; | 1247 TestingPlatformSupportWithMockScheduler platform; |
| 1129 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1248 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1130 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", | 1249 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", |
| 1131 "text/html"); | 1250 "text/html"); |
| 1132 ResourceRequest request = ResourceRequest(testURL); | 1251 ResourceRequest request = ResourceRequest(testURL); |
| 1133 ImageResource* cachedImage = ImageResource::create(request); | 1252 ImageResource* cachedImage = ImageResource::create(request); |
| 1134 cachedImage->setStatus(Resource::Pending); | 1253 cachedImage->setStatus(Resource::Pending); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1205 EXPECT_TRUE(client->notifyFinishedCalled()); | 1324 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1206 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); | 1325 EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage()); |
| 1207 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); | 1326 EXPECT_EQ(50, cachedImage->getContent()->getImage()->width()); |
| 1208 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); | 1327 EXPECT_EQ(50, cachedImage->getContent()->getImage()->height()); |
| 1209 | 1328 |
| 1210 WTF::setTimeFunctionsForTesting(nullptr); | 1329 WTF::setTimeFunctionsForTesting(nullptr); |
| 1211 } | 1330 } |
| 1212 | 1331 |
| 1213 } // namespace | 1332 } // namespace |
| 1214 } // namespace blink | 1333 } // namespace blink |
| OLD | NEW |