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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 464 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
464 | 465 |
465 // The observer should not have been notified of completion again. | 466 // The observer should not have been notified of completion again. |
466 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | 467 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
467 | 468 |
468 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 469 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
469 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 470 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
470 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 471 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
471 } | 472 } |
472 | 473 |
474 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderViaResourceFetcher) { | |
475 ResourceFetcher* fetcher = | |
476 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | |
477 | |
478 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest2.html"); | |
479 ScopedRegisteredURL scopedRegisteredURL(testURL); | |
480 | |
481 ResourceRequest request = ResourceRequest(testURL); | |
482 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | |
483 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | |
484 ImageResource* imageResource = ImageResource::fetch(fetchRequest, fetcher); | |
485 ImageResourceContent* content = imageResource->getContent(); | |
486 | |
487 std::unique_ptr<MockImageResourceObserver> observer = | |
488 MockImageResourceObserver::create(content); | |
489 | |
490 // Send the image response. | |
491 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage), | |
492 nullAtom, String()); | |
493 resourceResponse.addHTTPHeaderField("chrome-proxy-content-transform", | |
494 "empty-image"); | |
495 | |
496 imageResource->loader()->didReceiveResponse( | |
497 WrappedResourceResponse(resourceResponse)); | |
498 imageResource->loader()->didReceiveData( | |
499 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
500 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | |
501 sizeof(kJpegImage)); | |
502 | |
503 EXPECT_EQ(imageResource, fetcher->cachedResource(testURL)); | |
504 | |
505 fetcher->reloadLoFiImages(); | |
506 | |
507 EXPECT_FALSE(imageResource->errorOccurred()); | |
508 EXPECT_FALSE(imageResource->resourceBuffer()); | |
509 EXPECT_FALSE(imageResource->getContent()->hasImage()); | |
510 EXPECT_EQ(3, observer->imageChangedCount()); | |
511 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
512 | |
513 imageResource->loader()->didReceiveResponse( | |
514 WrappedResourceResponse(resourceResponse), nullptr); | |
515 imageResource->loader()->didReceiveData( | |
516 reinterpret_cast<const char*>(kJpegImage2), sizeof(kJpegImage2)); | |
517 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage2), | |
518 sizeof(kJpegImage2)); | |
519 EXPECT_FALSE(imageResource->errorOccurred()); | |
520 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
521 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | |
522 EXPECT_EQ(kJpegImage2Width, observer->imageWidthOnLastImageChanged()); | |
523 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
524 | |
525 // The observer should not have been notified of completion again. | |
526 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
527 | |
528 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
529 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | |
530 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | |
531 } | |
532 | |
473 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { | 533 TEST(ImageResourceTest, ReloadIfLoFiOrPlaceholderDuringFetch) { |
474 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 534 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
475 ScopedRegisteredURL scopedRegisteredURL(testURL); | 535 ScopedRegisteredURL scopedRegisteredURL(testURL); |
476 | 536 |
477 ResourceRequest request(testURL); | 537 ResourceRequest request(testURL); |
478 request.setPreviewsState(WebURLRequest::ServerLoFiOn); | 538 request.setPreviewsState(WebURLRequest::ServerLoFiOn); |
479 FetchRequest fetchRequest(request, FetchInitiatorInfo()); | 539 FetchRequest fetchRequest(request, FetchInitiatorInfo()); |
480 ResourceFetcher* fetcher = | 540 ResourceFetcher* fetcher = |
481 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 541 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
482 | 542 |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
898 imageResource->loader()->didFinishLoading(0.0, 0, 0); | 958 imageResource->loader()->didFinishLoading(0.0, 0, 0); |
899 | 959 |
900 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus()); | 960 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus()); |
901 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 961 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
902 EXPECT_EQ(ResourceStatus::DecodeError, | 962 EXPECT_EQ(ResourceStatus::DecodeError, |
903 observer->statusOnImageNotifyFinished()); | 963 observer->statusOnImageNotifyFinished()); |
904 EXPECT_EQ(1, observer->imageChangedCount()); | 964 EXPECT_EQ(1, observer->imageChangedCount()); |
905 EXPECT_FALSE(imageResource->isLoading()); | 965 EXPECT_FALSE(imageResource->isLoading()); |
906 } | 966 } |
907 | 967 |
968 // Testing DecodeError that occurs in didFinishLoading(). | |
969 // This is similar to DecodeErrorWithEmptyBody, but with non-empty body. | |
970 TEST(ImageResourceTest, PartialContentWithoutDimensions) { | |
971 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | |
972 ScopedRegisteredURL scopedRegisteredURL(testURL); | |
973 | |
974 FetchRequest request(testURL, FetchInitiatorInfo()); | |
975 ResourceFetcher* fetcher = | |
976 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | |
977 ImageResource* imageResource = ImageResource::fetch(request, fetcher); | |
978 std::unique_ptr<MockImageResourceObserver> observer = | |
979 MockImageResourceObserver::create(imageResource->getContent()); | |
980 | |
981 ResourceResponse partialResponse(testURL, "image/jpeg", | |
982 kJpegImageSubrangeWithoutDimensionsLength, | |
983 nullAtom, String()); | |
984 partialResponse.setHTTPStatusCode(206); | |
yhirano
2017/02/07 09:24:02
Why do you serve a partial response to a request w
hiroshige
2017/02/07 23:53:48
Fixed.
| |
985 partialResponse.setHTTPHeaderField( | |
986 "content-range", | |
987 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength, | |
988 sizeof(kJpegImage))); | |
989 | |
990 imageResource->loader()->didReceiveResponse( | |
991 WrappedResourceResponse(partialResponse)); | |
992 imageResource->loader()->didReceiveData( | |
993 reinterpret_cast<const char*>(kJpegImage), | |
994 kJpegImageSubrangeWithoutDimensionsLength); | |
995 | |
996 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
997 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | |
998 EXPECT_EQ(0, observer->imageChangedCount()); | |
999 | |
1000 imageResource->loader()->didFinishLoading( | |
1001 0.0, kJpegImageSubrangeWithoutDimensionsLength, | |
1002 kJpegImageSubrangeWithoutDimensionsLength); | |
1003 | |
1004 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus()); | |
1005 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
1006 EXPECT_EQ(ResourceStatus::DecodeError, | |
1007 observer->statusOnImageNotifyFinished()); | |
1008 EXPECT_EQ(1, observer->imageChangedCount()); | |
1009 EXPECT_FALSE(imageResource->isLoading()); | |
1010 } | |
1011 | |
908 TEST(ImageResourceTest, FetchDisallowPlaceholder) { | 1012 TEST(ImageResourceTest, FetchDisallowPlaceholder) { |
909 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1013 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
910 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1014 ScopedRegisteredURL scopedRegisteredURL(testURL); |
911 | 1015 |
912 FetchRequest request(testURL, FetchInitiatorInfo()); | 1016 FetchRequest request(testURL, FetchInitiatorInfo()); |
913 ImageResource* imageResource = ImageResource::fetch( | 1017 ImageResource* imageResource = ImageResource::fetch( |
914 request, | 1018 request, |
915 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | 1019 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
916 EXPECT_EQ(FetchRequest::DisallowPlaceholder, | 1020 EXPECT_EQ(FetchRequest::DisallowPlaceholder, |
917 request.placeholderImageRequestType()); | 1021 request.placeholderImageRequestType()); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1101 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | 1205 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
1102 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 1206 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
1103 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | 1207 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
1104 | 1208 |
1105 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1209 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
1106 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | 1210 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
1107 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | 1211 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
1108 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1212 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
1109 } | 1213 } |
1110 | 1214 |
1215 TEST(ImageResourceTest, FetchAllowPlaceholderPartialContentWithoutDimensions) { | |
yhirano
2017/02/07 09:24:02
The server sends a response consisting of 3 bytes
hiroshige
2017/02/07 23:53:48
Yes, strange, but in some of the other unit tests
sclittle
2017/02/08 00:32:59
+1 to filing a crbug and doing it later, I agree t
yhirano
2017/02/08 01:18:39
Please put some comments.
| |
1216 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | |
1217 ScopedRegisteredURL scopedRegisteredURL(testURL); | |
1218 | |
1219 FetchRequest request(testURL, FetchInitiatorInfo()); | |
1220 request.setAllowImagePlaceholder(); | |
1221 ImageResource* imageResource = ImageResource::fetch( | |
1222 request, | |
1223 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); | |
1224 EXPECT_EQ(FetchRequest::AllowPlaceholder, | |
1225 request.placeholderImageRequestType()); | |
1226 EXPECT_EQ("bytes=0-2047", | |
1227 imageResource->resourceRequest().httpHeaderField("range")); | |
1228 EXPECT_TRUE(imageResource->isPlaceholder()); | |
1229 std::unique_ptr<MockImageResourceObserver> observer = | |
1230 MockImageResourceObserver::create(imageResource->getContent()); | |
1231 | |
1232 ResourceResponse partialResponse(testURL, "image/jpeg", | |
1233 kJpegImageSubrangeWithoutDimensionsLength, | |
1234 nullAtom, String()); | |
1235 partialResponse.setHTTPStatusCode(206); | |
1236 partialResponse.setHTTPHeaderField( | |
1237 "content-range", | |
1238 buildContentRange(kJpegImageSubrangeWithoutDimensionsLength, | |
1239 sizeof(kJpegImage))); | |
1240 | |
1241 imageResource->loader()->didReceiveResponse( | |
1242 WrappedResourceResponse(partialResponse)); | |
1243 imageResource->loader()->didReceiveData( | |
1244 reinterpret_cast<const char*>(kJpegImage), | |
1245 kJpegImageSubrangeWithoutDimensionsLength); | |
1246 | |
1247 EXPECT_EQ(0, observer->imageChangedCount()); | |
1248 | |
1249 imageResource->loader()->didFinishLoading( | |
1250 0.0, kJpegImageSubrangeWithoutDimensionsLength, | |
1251 kJpegImageSubrangeWithoutDimensionsLength); | |
1252 | |
1253 // Decode error didn't occur but the dimensions could not be extracted, | |
1254 // so the full original image should be loading. | |
1255 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | |
1256 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1257 EXPECT_EQ(nullAtom, | |
1258 imageResource->resourceRequest().httpHeaderField("range")); | |
1259 EXPECT_EQ( | |
1260 static_cast<int>(WebCachePolicy::BypassingCache), | |
1261 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | |
1262 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); | |
1263 EXPECT_EQ(2, observer->imageChangedCount()); | |
1264 | |
1265 imageResource->loader()->didReceiveResponse( | |
1266 WrappedResourceResponse(ResourceResponse( | |
1267 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String()))); | |
1268 imageResource->loader()->didReceiveData( | |
1269 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); | |
1270 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), | |
1271 sizeof(kJpegImage)); | |
1272 | |
1273 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); | |
1274 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); | |
1275 EXPECT_FALSE(imageResource->isPlaceholder()); | |
1276 EXPECT_LT(0, observer->imageChangedCount()); | |
1277 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); | |
1278 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | |
1279 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); | |
1280 | |
1281 ASSERT_TRUE(imageResource->getContent()->hasImage()); | |
1282 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); | |
1283 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); | |
1284 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | |
1285 } | |
1286 | |
1111 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { | 1287 TEST(ImageResourceTest, FetchAllowPlaceholderThenDisallowPlaceholder) { |
1112 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1288 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
1113 ScopedRegisteredURL scopedRegisteredURL(testURL); | 1289 ScopedRegisteredURL scopedRegisteredURL(testURL); |
1114 | 1290 |
1115 ResourceFetcher* fetcher = | 1291 ResourceFetcher* fetcher = |
1116 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 1292 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
1117 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); | 1293 FetchRequest placeholderRequest(testURL, FetchInitiatorInfo()); |
1118 placeholderRequest.setAllowImagePlaceholder(); | 1294 placeholderRequest.setAllowImagePlaceholder(); |
1119 ImageResource* imageResource = | 1295 ImageResource* imageResource = |
1120 ImageResource::fetch(placeholderRequest, fetcher); | 1296 ImageResource::fetch(placeholderRequest, fetcher); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1270 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 1446 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
1271 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1447 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
1272 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 1448 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
1273 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 1449 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
1274 | 1450 |
1275 WTF::setTimeFunctionsForTesting(nullptr); | 1451 WTF::setTimeFunctionsForTesting(nullptr); |
1276 } | 1452 } |
1277 | 1453 |
1278 } // namespace | 1454 } // namespace |
1279 } // namespace blink | 1455 } // namespace blink |
OLD | NEW |