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

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

Issue 2648073002: Add more unit tests for placeholder reloading (Closed)
Patch Set: Rebase fix Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 90 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
92 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 92 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f,
93 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; 93 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9};
94 94
95 constexpr int kJpegImageWidth = 1; 95 constexpr int kJpegImageWidth = 1;
96 96
97 constexpr size_t kJpegImageSubrangeWithDimensionsLength = 97 constexpr size_t kJpegImageSubrangeWithDimensionsLength =
98 sizeof(kJpegImage) - 1; 98 sizeof(kJpegImage) - 1;
99 constexpr size_t kJpegImageSubrangeWithoutDimensionsLength = 3;
99 100
100 // Ensure that the image decoder can determine the dimensions of kJpegImage from 101 // Ensure that the image decoder can determine the dimensions of kJpegImage from
101 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test 102 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test
102 // fails, then the test data here probably needs to be updated. 103 // fails, then the test data here probably needs to be updated.
103 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) { 104 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) {
104 RefPtr<Image> image = BitmapImage::create(); 105 RefPtr<Image> image = BitmapImage::create();
105 EXPECT_EQ( 106 EXPECT_EQ(
106 Image::SizeAvailable, 107 Image::SizeAvailable,
107 image->setData(SharedBuffer::create( 108 image->setData(SharedBuffer::create(
108 kJpegImage, kJpegImageSubrangeWithDimensionsLength), 109 kJpegImage, kJpegImageSubrangeWithDimensionsLength),
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 445 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
445 446
446 // The observer should not have been notified of completion again. 447 // The observer should not have been notified of completion again.
447 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); 448 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
448 449
449 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 450 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
450 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); 451 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
451 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); 452 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
452 } 453 }
453 454
455 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) {
456 ResourceFetcher* fetcher =
457 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
458
459 KURL testURL(ParsedURLString, kTestURL);
460 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
461
462 ResourceRequest request = ResourceRequest(testURL);
463 request.setPreviewsState(WebURLRequest::ServerLoFiOn);
464 FetchRequest fetchRequest(request, FetchInitiatorInfo());
465 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher);
466 ImageResourceContent* content = imageResource->getContent();
467
468 std::unique_ptr<MockImageResourceObserver> observer =
469 MockImageResourceObserver::create(content);
470
471 // Send the image response.
472 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage),
473 nullAtom);
474 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform",
475 "empty-image");
476
477 imageResource->loader()->didReceiveResponse(
478 WrappedResourceResponse(resourceResponse));
479 imageResource->loader()->didReceiveData(
480 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
481 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
482 sizeof(kJpegImage));
483
484 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL));
485
486 fetcher->reloadLoFiImages();
487
488 EXPECT_FALSE(imageResource->errorOccurred());
489 EXPECT_FALSE(imageResource->resourceBuffer());
490 EXPECT_FALSE(imageResource->getContent()->hasImage());
491 EXPECT_EQ(3, observer->imageChangedCount());
492 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
493
494 imageResource->loader()->didReceiveResponse(
495 WrappedResourceResponse(resourceResponse), nullptr);
496 imageResource->loader()->didReceiveData(
497 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
498 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
499 sizeof(kJpegImage2));
500 EXPECT_FALSE(imageResource->errorOccurred());
501 ASSERT_TRUE(imageResource->getContent()->hasImage());
502 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
503 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged());
504 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
505
506 // The observer should not have been notified of completion again.
507 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
508
509 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
510 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
511 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
512
513 memoryCache()->remove(imageResource);
514 }
515
454 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { 516 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
455 KURL testURL(ParsedURLString, kTestURL); 517 KURL testURL(ParsedURLString, kTestURL);
456 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); 518 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
457 519
458 ResourceRequest request(testURL); 520 ResourceRequest request(testURL);
459 request.setPreviewsState(WebURLRequest::ServerLoFiOn); 521 request.setPreviewsState(WebURLRequest::ServerLoFiOn);
460 FetchRequest fetchRequest(request, FetchInitiatorInfo()); 522 FetchRequest fetchRequest(request, FetchInitiatorInfo());
461 ResourceFetcher* fetcher = 523 ResourceFetcher* fetcher =
462 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 524 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
463 525
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 imageResource->loader()->didFinishLoading(0.0, 0, 0); 940 imageResource->loader()->didFinishLoading(0.0, 0, 0);
879 941
880 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus()); 942 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus());
881 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 943 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
882 EXPECT_EQ(ResourceStatus::DecodeError, 944 EXPECT_EQ(ResourceStatus::DecodeError,
883 observer->statusOnImageNotifyFinished()); 945 observer->statusOnImageNotifyFinished());
884 EXPECT_EQ(1, observer->imageChangedCount()); 946 EXPECT_EQ(1, observer->imageChangedCount());
885 EXPECT_FALSE(imageResource->isLoading()); 947 EXPECT_FALSE(imageResource->isLoading());
886 } 948 }
887 949
950 // Testing DecodeError that occurs in didFinishLoading().
951 // This is similar to DecodeErrorWithEmptyBody, but with non-empty body.
952 TEST(ImageResourceTest, PartialContentWithoutDimensions) {
953 KURL testURL(ParsedURLString, kTestURL);
954 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
955
956 ResourceRequest resourceRequest(testURL);
957 resourceRequest.setHTTPHeaderField("range", "bytes=0-2");
958 FetchRequest request(resourceRequest, FetchInitiatorInfo());
959 ResourceFetcher* fetcher =
960 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
961 ImageResource* imageResource = ImageResource::fetch(request, fetcher);
962 std::unique_ptr<MockImageResourceObserver> observer =
963 MockImageResourceObserver::create(imageResource->getContent());
964
965 ResourceResponse partialResponse(testURL, "image/jpeg",
966 kJpegImageSubrangeWithoutDimensionsLength,
967 nullAtom);
968 partialResponse.setHTTPStatusCode(206);
969 partialResponse.setHTTPHeaderField(
970 "content-range",
971 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength,
972 sizeof(kJpegImage)));
973
974 imageResource->loader()->didReceiveResponse(
975 WrappedResourceResponse(partialResponse));
976 imageResource->loader()->didReceiveData(
977 reinterpret_cast<const char*>(kJpegImage),
978 kJpegImageSubrangeWithoutDimensionsLength);
979
980 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
981 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
982 EXPECT_EQ(0, observer->imageChangedCount());
983
984 imageResource->loader()->didFinishLoading(
985 0.0, kJpegImageSubrangeWithoutDimensionsLength,
986 kJpegImageSubrangeWithoutDimensionsLength);
987
988 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus());
989 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
990 EXPECT_EQ(ResourceStatus::DecodeError,
991 observer->statusOnImageNotifyFinished());
992 EXPECT_EQ(1, observer->imageChangedCount());
993 EXPECT_FALSE(imageResource->isLoading());
994 }
995
888 TEST(ImageResourceTest, FetchDisallowPlaceholder) { 996 TEST(ImageResourceTest, FetchDisallowPlaceholder) {
889 KURL testURL(ParsedURLString, kTestURL); 997 KURL testURL(ParsedURLString, kTestURL);
890 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); 998 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
891 999
892 FetchRequest request(testURL, FetchInitiatorInfo()); 1000 FetchRequest request(testURL, FetchInitiatorInfo());
893 ImageResource* imageResource = ImageResource::fetch( 1001 ImageResource* imageResource = ImageResource::fetch(
894 request, 1002 request,
895 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 1003 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
896 EXPECT_EQ(FetchRequest::DisallowPlaceholder, 1004 EXPECT_EQ(FetchRequest::DisallowPlaceholder,
897 request.placeholderImageRequestType()); 1005 request.placeholderImageRequestType());
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); 1185 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1078 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1186 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1079 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); 1187 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1080 1188
1081 ASSERT_TRUE(imageResource->getContent()->hasImage()); 1189 ASSERT_TRUE(imageResource->getContent()->hasImage());
1082 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); 1190 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1083 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); 1191 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
1084 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 1192 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1085 } 1193 }
1086 1194
1195 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) {
1196 KURL testURL(ParsedURLString, kTestURL);
1197 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
1198
1199 FetchRequest request(testURL, FetchInitiatorInfo());
1200 request.setAllowImagePlaceholder();
1201 ImageResource* imageResource = ImageResource::fetch(
1202 request,
1203 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1204 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1205 request.placeholderImageRequestType());
1206 EXPECT_EQ("bytes=0-2047",
1207 imageResource->resourceRequest().httpHeaderField("range"));
1208 EXPECT_TRUE(imageResource->isPlaceholder());
1209 std::unique_ptr<MockImageResourceObserver> observer =
1210 MockImageResourceObserver::create(imageResource->getContent());
1211
1212 // TODO(hiroshige): Make the range request header and partial content length
1213 // consistent. https://crbug.com/689760.
1214 ResourceResponse partialResponse(testURL, "image/jpeg",
1215 kJpegImageSubrangeWithoutDimensionsLength,
1216 nullAtom);
1217 partialResponse.setHTTPStatusCode(206);
1218 partialResponse.setHTTPHeaderField(
1219 "content-range",
1220 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength,
1221 sizeof(kJpegImage)));
1222
1223 imageResource->loader()->didReceiveResponse(
1224 WrappedResourceResponse(partialResponse));
1225 imageResource->loader()->didReceiveData(
1226 reinterpret_cast<const char*>(kJpegImage),
1227 kJpegImageSubrangeWithoutDimensionsLength);
1228
1229 EXPECT_EQ(0, observer->imageChangedCount());
1230
1231 imageResource->loader()->didFinishLoading(
1232 0.0, kJpegImageSubrangeWithoutDimensionsLength,
1233 kJpegImageSubrangeWithoutDimensionsLength);
1234
1235 // Decode error didn't occur but the dimensions could not be extracted,
1236 // so the full original image should be loading.
1237 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1238 EXPECT_FALSE(imageResource->isPlaceholder());
1239 EXPECT_EQ(nullAtom,
1240 imageResource->resourceRequest().httpHeaderField("range"));
1241 EXPECT_EQ(
1242 static_cast<int>(WebCachePolicy::BypassingCache),
1243 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1244 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1245 EXPECT_EQ(2, observer->imageChangedCount());
1246
1247 imageResource->loader()->didReceiveResponse(WrappedResourceResponse(
1248 ResourceResponse(testURL, "image/jpeg", sizeof(kJpegImage), nullAtom)));
1249 imageResource->loader()->didReceiveData(
1250 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1251 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1252 sizeof(kJpegImage));
1253
1254 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
1255 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1256 EXPECT_FALSE(imageResource->isPlaceholder());
1257 EXPECT_LT(0, observer->imageChangedCount());
1258 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1259 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1260 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1261
1262 ASSERT_TRUE(imageResource->getContent()->hasImage());
1263 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1264 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
1265 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1266 }
1267
1087 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { 1268 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
1088 KURL testURL(ParsedURLString, kTestURL); 1269 KURL testURL(ParsedURLString, kTestURL);
1089 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); 1270 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath());
1090 1271
1091 ResourceFetcher* fetcher = 1272 ResourceFetcher* fetcher =
1092 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1273 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1093 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1274 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1094 placeholderRequest.setAllowImagePlaceholder(); 1275 placeholderRequest.setAllowImagePlaceholder();
1095 ImageResource* imageResource = 1276 ImageResource* imageResource =
1096 ImageResource::fetch(placeholderRequest, fetcher); 1277 ImageResource::fetch(placeholderRequest, fetcher);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1426 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1246 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 1427 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1247 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); 1428 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
1248 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); 1429 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
1249 1430
1250 WTF::setTimeFunctionsForTesting(nullptr); 1431 WTF::setTimeFunctionsForTesting(nullptr);
1251 } 1432 }
1252 1433
1253 } // namespace 1434 } // namespace
1254 } // namespace blink 1435 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698