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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp
diff --git a/third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp b/third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp
index 4711840a933dd1d7e28d5dabf4fed73b74ee9978..228be259c1b1d5e43f495baf02c7d2e9a9d33883 100644
--- a/third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp
+++ b/third_party/WebKit/Source/core/fetch/ImageResourceTest.cpp
@@ -316,7 +316,7 @@ TEST(ImageResourceTest, CancelOnDetach) {
// The load should still be alive, but a timer should be started to cancel the
// load inside removeClient().
- client->removeAsClient();
+ client->removeAsClientAndObserver();
EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
EXPECT_TRUE(memoryCache()->resourceForURL(testURL));
@@ -363,7 +363,7 @@ TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) {
// The ImageResource no longer has clients. The decoded image data should be
// deleted by prune.
- client->removeAsClient();
+ client->removeAsClientAndObserver();
cachedImage->prune();
EXPECT_FALSE(cachedImage->isAlive());
EXPECT_TRUE(cachedImage->getContent()->hasImage());
@@ -395,17 +395,19 @@ TEST(ImageResourceTest, UpdateBitmapImages) {
}
TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) {
+ ResourceFetcher* fetcher =
+ ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
+
KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
ScopedRegisteredURL scopedRegisteredURL(testURL);
ResourceRequest request = ResourceRequest(testURL);
request.setLoFiState(WebURLRequest::LoFiOn);
ImageResource* cachedImage = ImageResource::create(request);
+ ImageResourceContent* content = cachedImage->getContent();
cachedImage->setStatus(Resource::Pending);
Persistent<MockImageResourceClient> client =
new MockImageResourceClient(cachedImage);
- ResourceFetcher* fetcher =
- ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
// Send the image response.
ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage),
@@ -417,9 +419,9 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) {
cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage),
sizeof(kJpegImage));
cachedImage->finish();
- EXPECT_FALSE(cachedImage->errorOccurred());
- ASSERT_TRUE(cachedImage->getContent()->hasImage());
- EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
+ EXPECT_FALSE(content->errorOccurred());
+ ASSERT_TRUE(content->hasImage());
+ EXPECT_FALSE(content->getImage()->isNull());
EXPECT_EQ(2, client->imageChangedCount());
EXPECT_EQ(1, client->imageNotifyFinishedCount());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
@@ -427,38 +429,43 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderAfterFinished) {
EXPECT_TRUE(client->notifyFinishedCalled());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
- EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
- EXPECT_EQ(1, cachedImage->getContent()->getImage()->width());
- EXPECT_EQ(1, cachedImage->getContent()->getImage()->height());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
+ EXPECT_EQ(1, content->getImage()->width());
+ EXPECT_EQ(1, content->getImage()->height());
// Call reloadIfLoFiOrPlaceholder() after the image has finished loading.
- cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
- EXPECT_FALSE(cachedImage->errorOccurred());
- EXPECT_FALSE(cachedImage->resourceBuffer());
- EXPECT_FALSE(cachedImage->getContent()->hasImage());
+ ImageResource* reloadingResource =
+ cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
+ EXPECT_FALSE(cachedImage->getContent());
+ EXPECT_FALSE(content->errorOccurred());
+ EXPECT_TRUE(content->hasImage());
EXPECT_EQ(2, client->imageChangedCount());
EXPECT_EQ(1, client->imageNotifyFinishedCount());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
+ EXPECT_EQ(1, content->getImage()->width());
+ EXPECT_EQ(1, content->getImage()->height());
+ ASSERT_TRUE(reloadingResource);
- cachedImage->loader()->didReceiveResponse(
+ reloadingResource->loader()->didReceiveResponse(
WrappedResourceResponse(resourceResponse), nullptr);
- cachedImage->loader()->didReceiveData(
+ reloadingResource->loader()->didReceiveData(
reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2),
sizeof(kJpegImage2));
- cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
- sizeof(kJpegImage2));
- EXPECT_FALSE(cachedImage->errorOccurred());
- ASSERT_TRUE(cachedImage->getContent()->hasImage());
- EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
+ reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
+ sizeof(kJpegImage2));
+ EXPECT_FALSE(content->errorOccurred());
+ ASSERT_TRUE(content->hasImage());
+ EXPECT_FALSE(content->getImage()->isNull());
+ EXPECT_EQ(3, client->imageChangedCount());
EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
- EXPECT_TRUE(client->notifyFinishedCalled());
- // The client should not have been notified of completion again.
- EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
+ // The observer should not have been notified of completion again.
+ EXPECT_EQ(1, client->imageNotifyFinishedCount());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
- EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
- EXPECT_EQ(50, cachedImage->getContent()->getImage()->width());
- EXPECT_EQ(50, cachedImage->getContent()->getImage()->height());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
+ EXPECT_EQ(50, content->getImage()->width());
+ EXPECT_EQ(50, content->getImage()->height());
}
TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
@@ -472,9 +479,15 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
ImageResource* cachedImage = ImageResource::fetch(fetchRequest, fetcher);
+ ImageResourceContent* content = cachedImage->getContent();
Persistent<MockImageResourceClient> client =
new MockImageResourceClient(cachedImage);
+ // To test that the old ImageResource is cancelled if it has no
+ // ResourceClient when reloading is started, we remove |client| as client
+ // here, but not as observer.
+ client->removeAsClient();
+
// Send the image response.
ResourceResponse initialResourceResponse(
testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String());
@@ -486,49 +499,60 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage),
sizeof(kJpegImage));
- EXPECT_FALSE(cachedImage->errorOccurred());
- ASSERT_TRUE(cachedImage->getContent()->hasImage());
- EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
+ EXPECT_FALSE(content->errorOccurred());
+ ASSERT_TRUE(content->hasImage());
+ EXPECT_FALSE(content->getImage()->isNull());
EXPECT_EQ(1, client->imageChangedCount());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
- EXPECT_FALSE(client->notifyFinishedCalled());
- EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
- EXPECT_EQ(1, cachedImage->getContent()->getImage()->width());
- EXPECT_EQ(1, cachedImage->getContent()->getImage()->height());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
+ EXPECT_EQ(1, content->getImage()->width());
+ EXPECT_EQ(1, content->getImage()->height());
// Call reloadIfLoFiOrPlaceholder() while the image is still loading.
- cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
- EXPECT_FALSE(cachedImage->errorOccurred());
- EXPECT_FALSE(cachedImage->resourceBuffer());
- EXPECT_FALSE(cachedImage->getContent()->hasImage());
- EXPECT_EQ(2, client->imageChangedCount());
- EXPECT_EQ(0U, client->encodedSizeOnLastImageChanged());
+ ImageResource* reloadingResource =
+ cachedImage->reloadIfLoFiOrPlaceholder(fetcher);
+ EXPECT_FALSE(cachedImage->getContent());
+ EXPECT_FALSE(content->errorOccurred());
+ // ImageResourceContent is not notified of the start of LoFi reloading.
+ EXPECT_TRUE(content->hasImage());
+ EXPECT_EQ(1, client->imageChangedCount());
+ EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
+ EXPECT_EQ(1, content->getImage()->width());
+ EXPECT_EQ(1, content->getImage()->height());
// The client should not have been notified of completion yet, since the image
// is still loading.
- EXPECT_FALSE(client->notifyFinishedCalled());
+ EXPECT_EQ(0, client->imageNotifyFinishedCount());
+ ASSERT_TRUE(reloadingResource);
- cachedImage->loader()->didReceiveResponse(
+ reloadingResource->loader()->didReceiveResponse(
WrappedResourceResponse(ResourceResponse(
testURL, "image/jpeg", sizeof(kJpegImage2), nullAtom, String())),
nullptr);
- cachedImage->loader()->didReceiveData(
+ reloadingResource->loader()->didReceiveData(
reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2),
sizeof(kJpegImage2));
- cachedImage->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
- sizeof(kJpegImage2));
+ reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
+ sizeof(kJpegImage2));
- EXPECT_FALSE(cachedImage->errorOccurred());
- ASSERT_TRUE(cachedImage->getContent()->hasImage());
- EXPECT_FALSE(cachedImage->getContent()->getImage()->isNull());
+ EXPECT_FALSE(content->errorOccurred());
+ // ImageResourceContent is notified of reloaded response.
+ ASSERT_TRUE(content->hasImage());
+ EXPECT_EQ(1, client->imageNotifyFinishedCount());
+ EXPECT_EQ(2, client->imageChangedCount());
+ EXPECT_FALSE(content->getImage()->isNull());
EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnLastImageChanged());
- // The client should have been notified of completion only after the reload
+ // The observer should have been notified of completion only after the reload
// completed.
- EXPECT_TRUE(client->notifyFinishedCalled());
- EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnNotifyFinished());
EXPECT_EQ(sizeof(kJpegImage2), client->encodedSizeOnImageNotifyFinished());
- EXPECT_TRUE(cachedImage->getContent()->getImage()->isBitmapImage());
- EXPECT_EQ(50, cachedImage->getContent()->getImage()->width());
- EXPECT_EQ(50, cachedImage->getContent()->getImage()->height());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
+ EXPECT_EQ(50, content->getImage()->width());
+ EXPECT_EQ(50, content->getImage()->height());
+
+ // Trigger the cancel timer, ensure the load for the first ImageResource
+ // was cancelled because reloading is started using another ImageResource.
+ EXPECT_EQ(Resource::Pending, cachedImage->getStatus());
+ blink::testing::runPendingTasks();
+ EXPECT_EQ(Resource::LoadError, cachedImage->getStatus());
hiroshige 2016/12/05 10:12:40 Tests that the old ImageResource is cancelled afte
}
TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
@@ -540,11 +564,14 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
FetchRequest request(testURL, FetchInitiatorInfo());
request.setAllowImagePlaceholder();
ImageResource* image = ImageResource::fetch(request, fetcher);
+ ImageResourceContent* content = image->getContent();
+ ASSERT_TRUE(content);
+ Persistent<MockImageResourceClient> client =
+ new MockImageResourceClient(image);
+
EXPECT_EQ(FetchRequest::AllowPlaceholder,
request.placeholderImageRequestType());
EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"));
- Persistent<MockImageResourceClient> client =
- new MockImageResourceClient(image);
ResourceResponse response(testURL, "image/jpeg",
kJpegImageSubrangeWithDimensionsLength, nullAtom,
@@ -563,15 +590,19 @@ TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderForPlaceholder) {
EXPECT_EQ(Resource::Cached, image->getStatus());
EXPECT_TRUE(image->isPlaceholder());
- image->reloadIfLoFiOrPlaceholder(fetcher);
+ ImageResource* reloadingResource = image->reloadIfLoFiOrPlaceholder(fetcher);
- EXPECT_EQ(Resource::Pending, image->getStatus());
- EXPECT_FALSE(image->isPlaceholder());
- EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
- EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache),
- static_cast<int>(image->resourceRequest().getCachePolicy()));
+ EXPECT_FALSE(image->getContent());
+ ASSERT_TRUE(reloadingResource);
+ EXPECT_EQ(Resource::Pending, content->getStatus());
+ EXPECT_FALSE(reloadingResource->isPlaceholder());
+ EXPECT_EQ(nullAtom,
+ reloadingResource->resourceRequest().httpHeaderField("range"));
+ EXPECT_EQ(
+ static_cast<int>(WebCachePolicy::BypassingCache),
+ static_cast<int>(reloadingResource->resourceRequest().getCachePolicy()));
- image->loader()->cancel();
+ reloadingResource->loader()->cancel();
}
TEST(ImageResourceTest, SVGImage) {
@@ -1011,6 +1042,7 @@ TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) {
ImageResource* image = ImageResource::fetch(
request,
ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
+ ImageResourceContent* content = image->getContent();
EXPECT_EQ(FetchRequest::AllowPlaceholder,
request.placeholderImageRequestType());
EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"));
@@ -1025,38 +1057,44 @@ TEST(ImageResourceTest, FetchAllowPlaceholderUnsuccessful) {
testURL, "image/jpeg", sizeof(kBadData), nullAtom, String())));
image->loader()->didReceiveData(kBadData, sizeof(kBadData), sizeof(kBadData));
+ ImageResource* reloadingResource = content->resourceForTest();
+ ASSERT_TRUE(reloadingResource);
+ EXPECT_NE(image, reloadingResource);
+ EXPECT_FALSE(image->getContent());
+ EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
+
// The dimensions could not be extracted, so the full original image should be
// loading.
- EXPECT_EQ(Resource::Pending, image->getStatus());
- EXPECT_FALSE(image->isPlaceholder());
- EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
- EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache),
- static_cast<int>(image->resourceRequest().getCachePolicy()));
- EXPECT_FALSE(client->notifyFinishedCalled());
+ EXPECT_EQ(Resource::Pending, content->getStatus());
+ EXPECT_FALSE(reloadingResource->isPlaceholder());
+ EXPECT_EQ(nullAtom,
+ reloadingResource->resourceRequest().httpHeaderField("range"));
+ EXPECT_EQ(
+ static_cast<int>(WebCachePolicy::BypassingCache),
+ static_cast<int>(reloadingResource->resourceRequest().getCachePolicy()));
EXPECT_EQ(0, client->imageNotifyFinishedCount());
- image->loader()->didReceiveResponse(
+ reloadingResource->loader()->didReceiveResponse(
WrappedResourceResponse(ResourceResponse(
testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
- image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
- sizeof(kJpegImage), sizeof(kJpegImage));
- image->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
- sizeof(kJpegImage));
+ reloadingResource->loader()->didReceiveData(
+ reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage),
+ sizeof(kJpegImage));
+ reloadingResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
+ sizeof(kJpegImage));
- EXPECT_EQ(Resource::Cached, image->getStatus());
- EXPECT_EQ(sizeof(kJpegImage), image->encodedSize());
- EXPECT_FALSE(image->isPlaceholder());
+ EXPECT_EQ(Resource::Cached, content->getStatus());
+ EXPECT_EQ(sizeof(kJpegImage), reloadingResource->encodedSize());
+ EXPECT_FALSE(reloadingResource->isPlaceholder());
EXPECT_LT(0, client->imageChangedCount());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
- EXPECT_TRUE(client->notifyFinishedCalled());
EXPECT_EQ(1, client->imageNotifyFinishedCount());
- EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
- ASSERT_TRUE(image->getContent()->hasImage());
- EXPECT_EQ(1, image->getContent()->getImage()->width());
- EXPECT_EQ(1, image->getContent()->getImage()->height());
- EXPECT_TRUE(image->getContent()->getImage()->isBitmapImage());
+ ASSERT_TRUE(content->hasImage());
+ EXPECT_EQ(1, content->getImage()->width());
+ EXPECT_EQ(1, content->getImage()->height());
+ EXPECT_TRUE(content->getImage()->isBitmapImage());
}
TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
@@ -1068,21 +1106,26 @@ TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
placeholderRequest.setAllowImagePlaceholder();
ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher);
+ ImageResourceContent* content = image->getContent();
Persistent<MockImageResourceClient> client =
new MockImageResourceClient(image);
FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
ImageResource* secondImage =
ImageResource::fetch(nonPlaceholderRequest, fetcher);
- EXPECT_EQ(image, secondImage);
- EXPECT_EQ(Resource::Pending, image->getStatus());
- EXPECT_FALSE(image->isPlaceholder());
- EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
+ EXPECT_NE(image, secondImage);
+ ASSERT_EQ(secondImage, content->resourceForTest());
+ EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
+ EXPECT_EQ(content, secondImage->getContent());
+ EXPECT_EQ(Resource::Pending, content->getStatus());
+ EXPECT_FALSE(secondImage->isPlaceholder());
+ EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range"));
EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
- static_cast<int>(image->resourceRequest().getCachePolicy()));
+ static_cast<int>(secondImage->resourceRequest().getCachePolicy()));
EXPECT_FALSE(client->notifyFinishedCalled());
image->loader()->cancel();
+ secondImage->loader()->cancel();
}
TEST(ImageResourceTest,
@@ -1095,6 +1138,7 @@ TEST(ImageResourceTest,
FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
placeholderRequest.setAllowImagePlaceholder();
ImageResource* image = ImageResource::fetch(placeholderRequest, fetcher);
+ ImageResourceContent* content = image->getContent();
Persistent<MockImageResourceClient> client =
new MockImageResourceClient(image);
@@ -1121,14 +1165,18 @@ TEST(ImageResourceTest,
FetchRequest nonPlaceholderRequest(testURL, FetchInitiatorInfo());
ImageResource* secondImage =
ImageResource::fetch(nonPlaceholderRequest, fetcher);
- EXPECT_EQ(image, secondImage);
- EXPECT_EQ(Resource::Pending, image->getStatus());
- EXPECT_FALSE(image->isPlaceholder());
- EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
+ EXPECT_FALSE(image->getContent());
+ EXPECT_NE(image, secondImage);
+ EXPECT_FALSE(memoryCache()->resourceForURL(testURL));
+ ASSERT_EQ(secondImage, content->resourceForTest());
+ EXPECT_EQ(content, secondImage->getContent());
+ EXPECT_EQ(Resource::Pending, content->getStatus());
+ EXPECT_FALSE(secondImage->isPlaceholder());
+ EXPECT_EQ(nullAtom, secondImage->resourceRequest().httpHeaderField("range"));
EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
- static_cast<int>(image->resourceRequest().getCachePolicy()));
+ static_cast<int>(secondImage->resourceRequest().getCachePolicy()));
- image->loader()->cancel();
+ secondImage->loader()->cancel();
}
TEST(ImageResourceTest, PeriodicFlushTest) {
« 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