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

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 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 88 0x00, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
90 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 90 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f,
91 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9}; 91 0x00, 0xb2, 0xc0, 0x07, 0xff, 0xd9};
92 92
93 constexpr int kJpegImageWidth = 1; 93 constexpr int kJpegImageWidth = 1;
94 94
95 constexpr size_t kJpegImageSubrangeWithDimensionsLength = 95 constexpr size_t kJpegImageSubrangeWithDimensionsLength =
96 sizeof(kJpegImage) - 1; 96 sizeof(kJpegImage) - 1;
97 constexpr size_t kJpegImageSubrangeWithoutDimensionsLength = 3;
97 98
98 // Ensure that the image decoder can determine the dimensions of kJpegImage from 99 // Ensure that the image decoder can determine the dimensions of kJpegImage from
99 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test 100 // just the first kJpegImageSubrangeWithDimensionsLength bytes. If this test
100 // fails, then the test data here probably needs to be updated. 101 // fails, then the test data here probably needs to be updated.
101 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) { 102 TEST(ImageResourceTest, DimensionsDecodableFromPartialTestImage) {
102 RefPtr<Image> image = BitmapImage::create(); 103 RefPtr<Image> image = BitmapImage::create();
103 EXPECT_EQ( 104 EXPECT_EQ(
104 Image::SizeAvailable, 105 Image::SizeAvailable,
105 image->setData(SharedBuffer::create( 106 image->setData(SharedBuffer::create(
106 kJpegImage, kJpegImageSubrangeWithDimensionsLength), 107 kJpegImage, kJpegImageSubrangeWithDimensionsLength),
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 462 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
462 463
463 // The observer should not have been notified of completion again. 464 // The observer should not have been notified of completion again.
464 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); 465 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
465 466
466 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 467 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
467 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); 468 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
468 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); 469 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
469 } 470 }
470 471
472 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) {
473 ResourceFetcher* fetcher =
474 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
475
476 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest2.html");
477 ScopedRegisteredURL scopedRegisteredURL(testURL);
478
479 ResourceRequest request = ResourceRequest(testURL);
480 request.setPreviewsState(WebURLRequest::ServerLoFiOn);
481 FetchRequest fetchRequest(request, FetchInitiatorInfo());
482 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher);
483 ImageResourceContent* content = imageResource->getContent();
484
485 std::unique_ptr<MockImageResourceObserver> observer =
486 MockImageResourceObserver::create(content);
487
488 // Send the image response.
489 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage),
490 nullAtom, String());
491 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform",
492 "empty-image");
493
494 imageResource->loader()->didReceiveResponse(
495 WrappedResourceResponse(resourceResponse));
496 imageResource->loader()->didReceiveData(
497 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
498 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
499 sizeof(kJpegImage));
500
501 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL));
502
503 fetcher->reloadLoFiImages();
504
505 EXPECT_FALSE(imageResource->errorOccurred());
506 EXPECT_FALSE(imageResource->resourceBuffer());
507 EXPECT_FALSE(imageResource->getContent()->hasImage());
508 EXPECT_EQ(3, observer->imageChangedCount());
509 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
510
511 imageResource->loader()->didReceiveResponse(
512 WrappedResourceResponse(resourceResponse), nullptr);
513 imageResource->loader()->didReceiveData(
514 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2));
515 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2),
516 sizeof(kJpegImage2));
517 EXPECT_FALSE(imageResource->errorOccurred());
518 ASSERT_TRUE(imageResource->getContent()->hasImage());
519 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
520 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged());
521 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
522
523 // The observer should not have been notified of completion again.
524 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
525
526 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
527 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
528 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
529 }
530
471 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { 531 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) {
472 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 532 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
473 ScopedRegisteredURL scopedRegisteredURL(testURL); 533 ScopedRegisteredURL scopedRegisteredURL(testURL);
474 534
475 ResourceRequest request(testURL); 535 ResourceRequest request(testURL);
476 request.setPreviewsState(WebURLRequest::ServerLoFiOn); 536 request.setPreviewsState(WebURLRequest::ServerLoFiOn);
477 FetchRequest fetchRequest(request, FetchInitiatorInfo()); 537 FetchRequest fetchRequest(request, FetchInitiatorInfo());
478 ResourceFetcher* fetcher = 538 ResourceFetcher* fetcher =
479 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 539 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
480 540
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); 1159 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1100 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1160 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1101 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); 1161 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1102 1162
1103 ASSERT_TRUE(imageResource->getContent()->hasImage()); 1163 ASSERT_TRUE(imageResource->getContent()->hasImage());
1104 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); 1164 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1105 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); 1165 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
1106 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 1166 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1107 } 1167 }
1108 1168
1169 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) {
1170 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1171 ScopedRegisteredURL scopedRegisteredURL(testURL);
1172
1173 FetchRequest request(testURL, FetchInitiatorInfo());
1174 request.setAllowImagePlaceholder();
1175 ImageResource* imageResource = ImageResource::fetch(
1176 request,
1177 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1178 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1179 request.placeholderImageRequestType());
1180 EXPECT_EQ("bytes=0-2047",
1181 imageResource->resourceRequest().httpHeaderField("range"));
1182 EXPECT_TRUE(imageResource->isPlaceholder());
1183 std::unique_ptr<MockImageResourceObserver> observer =
1184 MockImageResourceObserver::create(imageResource->getContent());
1185
1186 ResourceResponse partialResponse(testURL, "image/jpeg",
1187 kJpegImageSubrangeWithoutDimensionsLength,
1188 nullAtom, String());
1189 partialResponse.setHTTPStatusCode(206);
1190 partialResponse.setHTTPHeaderField(
1191 "content-range",
1192 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength,
1193 sizeof(kJpegImage)));
1194
1195 imageResource->loader()->didReceiveResponse(
1196 WrappedResourceResponse(partialResponse));
1197 imageResource->loader()->didReceiveData(
1198 reinterpret_cast<const char*>(kJpegImage),
1199 kJpegImageSubrangeWithoutDimensionsLength);
1200
1201 EXPECT_EQ(0, observer->imageChangedCount());
1202
1203 imageResource->loader()->didFinishLoading(
1204 0.0, kJpegImageSubrangeWithoutDimensionsLength,
1205 kJpegImageSubrangeWithoutDimensionsLength);
1206
1207 // Decode error didn't occur but the dimensions could not be extracted,
1208 // so the full original image should be loading.
1209 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1210 EXPECT_FALSE(imageResource->isPlaceholder());
1211 EXPECT_EQ(nullAtom,
1212 imageResource->resourceRequest().httpHeaderField("range"));
1213 EXPECT_EQ(
1214 static_cast<int>(WebCachePolicy::BypassingCache),
1215 static_cast<int>(imageResource->resourceRequest().getCachePolicy()));
1216 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1217 EXPECT_EQ(2, observer->imageChangedCount());
1218
1219 imageResource->loader()->didReceiveResponse(
1220 WrappedResourceResponse(ResourceResponse(
1221 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1222 imageResource->loader()->didReceiveData(
1223 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage));
1224 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1225 sizeof(kJpegImage));
1226
1227 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus());
1228 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize());
1229 EXPECT_FALSE(imageResource->isPlaceholder());
1230 EXPECT_LT(0, observer->imageChangedCount());
1231 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged());
1232 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1233 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished());
1234
1235 ASSERT_TRUE(imageResource->getContent()->hasImage());
1236 EXPECT_EQ(1, imageResource->getContent()->getImage()->width());
1237 EXPECT_EQ(1, imageResource->getContent()->getImage()->height());
1238 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1239 }
1240
1241 // Testing DecodeError that occurs in didFinishLoading().
1242 // This is similar to DecodeErrorWithEmptyBody, but with non-empty body.
1243 TEST(ImageResourceTest, PartialContentWithoutDimensions) {
sclittle 2017/01/27 21:56:06 nit: Would it make sense to move this test up abov
hiroshige 2017/02/06 22:50:45 Done.
1244 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1245 ScopedRegisteredURL scopedRegisteredURL(testURL);
1246
1247 FetchRequest request(testURL, FetchInitiatorInfo());
1248 ResourceFetcher* fetcher =
1249 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1250 ImageResource* imageResource = ImageResource::fetch(request, fetcher);
1251 std::unique_ptr<MockImageResourceObserver> observer =
1252 MockImageResourceObserver::create(imageResource->getContent());
1253
1254 ResourceResponse partialResponse(testURL, "image/jpeg",
1255 kJpegImageSubrangeWithoutDimensionsLength,
1256 nullAtom, String());
1257 partialResponse.setHTTPStatusCode(206);
1258 partialResponse.setHTTPHeaderField(
1259 "content-range",
1260 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength,
1261 sizeof(kJpegImage)));
1262
1263 imageResource->loader()->didReceiveResponse(
1264 WrappedResourceResponse(partialResponse));
1265 imageResource->loader()->didReceiveData(
1266 reinterpret_cast<const char*>(kJpegImage),
1267 kJpegImageSubrangeWithoutDimensionsLength);
1268
1269 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus());
1270 EXPECT_FALSE(observer->imageNotifyFinishedCalled());
1271 EXPECT_EQ(0, observer->imageChangedCount());
1272
1273 imageResource->loader()->didFinishLoading(
1274 0.0, kJpegImageSubrangeWithoutDimensionsLength,
1275 kJpegImageSubrangeWithoutDimensionsLength);
1276
1277 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus());
1278 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1279 EXPECT_EQ(ResourceStatus::DecodeError,
1280 observer->statusOnImageNotifyFinished());
1281 EXPECT_EQ(1, observer->imageChangedCount());
1282 EXPECT_FALSE(imageResource->isLoading());
1283 }
1284
1109 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { 1285 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) {
1110 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1286 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1111 ScopedRegisteredURL scopedRegisteredURL(testURL); 1287 ScopedRegisteredURL scopedRegisteredURL(testURL);
1112 1288
1113 ResourceFetcher* fetcher = 1289 ResourceFetcher* fetcher =
1114 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 1290 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
1115 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); 1291 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo());
1116 placeholderRequest.setAllowImagePlaceholder(); 1292 placeholderRequest.setAllowImagePlaceholder();
1117 ImageResource* imageResource = 1293 ImageResource* imageResource =
1118 ImageResource::fetch(placeholderRequest, fetcher); 1294 ImageResource::fetch(placeholderRequest, fetcher);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1269 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); 1445 EXPECT_TRUE(observer->imageNotifyFinishedCalled());
1270 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); 1446 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage());
1271 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); 1447 EXPECT_EQ(50, imageResource->getContent()->getImage()->width());
1272 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); 1448 EXPECT_EQ(50, imageResource->getContent()->getImage()->height());
1273 1449
1274 WTF::setTimeFunctionsForTesting(nullptr); 1450 WTF::setTimeFunctionsForTesting(nullptr);
1275 } 1451 }
1276 1452
1277 } // namespace 1453 } // namespace
1278 } // namespace blink 1454 } // 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