| 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 RefPtr<WebTaskRunner> loadingTaskRunner() const override { return m_runner; } | 199 RefPtr<WebTaskRunner> loadingTaskRunner() const override { return m_runner; } |
| 200 | 200 |
| 201 private: | 201 private: |
| 202 ImageResourceTestMockFetchContext() | 202 ImageResourceTestMockFetchContext() |
| 203 : m_runner(adoptRef(new scheduler::FakeWebTaskRunner)) {} | 203 : m_runner(adoptRef(new scheduler::FakeWebTaskRunner)) {} |
| 204 | 204 |
| 205 RefPtr<scheduler::FakeWebTaskRunner> m_runner; | 205 RefPtr<scheduler::FakeWebTaskRunner> m_runner; |
| 206 }; | 206 }; |
| 207 | 207 |
| 208 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { | 208 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { |
| 209 return AtomicString(String("bytes 0-" + String::number(rangeLength) + "/" + | 209 return AtomicString(String("bytes 0-" + String::number(rangeLength - 1) + |
| 210 String::number(totalLength))); | 210 "/" + String::number(totalLength))); |
| 211 } | 211 } |
| 212 | 212 |
| 213 TEST(ImageResourceTest, MultipartImage) { | 213 TEST(ImageResourceTest, MultipartImage) { |
| 214 ResourceFetcher* fetcher = | 214 ResourceFetcher* fetcher = |
| 215 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); | 215 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); |
| 216 KURL testURL(ParsedURLString, kTestURL); | 216 KURL testURL(ParsedURLString, kTestURL); |
| 217 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 217 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
| 218 | 218 |
| 219 // Emulate starting a real load, but don't expect any "real" | 219 // Emulate starting a real load, but don't expect any "real" |
| 220 // WebURLLoaderClient callbacks. | 220 // WebURLLoaderClient callbacks. |
| (...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1144 EXPECT_EQ(FetchRequest::AllowPlaceholder, | 1144 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1145 request.placeholderImageRequestType()); | 1145 request.placeholderImageRequestType()); |
| 1146 EXPECT_EQ("bytes=0-2047", | 1146 EXPECT_EQ("bytes=0-2047", |
| 1147 imageResource->resourceRequest().httpHeaderField("range")); | 1147 imageResource->resourceRequest().httpHeaderField("range")); |
| 1148 EXPECT_TRUE(imageResource->isPlaceholder()); | 1148 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1149 std::unique_ptr<MockImageResourceObserver> observer = | 1149 std::unique_ptr<MockImageResourceObserver> observer = |
| 1150 MockImageResourceObserver::create(imageResource->getContent()); | 1150 MockImageResourceObserver::create(imageResource->getContent()); |
| 1151 | 1151 |
| 1152 const char kBadData[] = "notanimageresponse"; | 1152 const char kBadData[] = "notanimageresponse"; |
| 1153 | 1153 |
| 1154 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( | 1154 ResourceResponse badResponse(testURL, "image/jpeg", sizeof(kBadData), |
| 1155 ResourceResponse(testURL, "image/jpeg", sizeof(kBadData), nullAtom))); | 1155 nullAtom); |
| 1156 badResponse.setHTTPStatusCode(206); |
| 1157 badResponse.setHTTPHeaderField( |
| 1158 "content-range", buildContentRange(sizeof(kBadData), sizeof(kJpegImage))); |
| 1159 |
| 1160 imageResource->loader()->didReceiveResponse( |
| 1161 WrappedResourceResponse(badResponse)); |
| 1156 | 1162 |
| 1157 EXPECT_EQ(0, observer->imageChangedCount()); | 1163 EXPECT_EQ(0, observer->imageChangedCount()); |
| 1158 | 1164 |
| 1159 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); | 1165 imageResource->loader()->didReceiveData(kBadData, sizeof(kBadData)); |
| 1160 | 1166 |
| 1161 // The dimensions could not be extracted, so the full original image should be | 1167 // The dimensions could not be extracted, so the full original image should be |
| 1162 // loading. | 1168 // loading. |
| 1163 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); | 1169 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); |
| 1164 EXPECT_FALSE(imageResource->isPlaceholder()); | 1170 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1165 EXPECT_EQ(nullAtom, | 1171 EXPECT_EQ(nullAtom, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1337 EXPECT_FALSE(imageResource->isPlaceholder()); | 1343 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1338 EXPECT_EQ(nullAtom, | 1344 EXPECT_EQ(nullAtom, |
| 1339 imageResource->resourceRequest().httpHeaderField("range")); | 1345 imageResource->resourceRequest().httpHeaderField("range")); |
| 1340 EXPECT_EQ( | 1346 EXPECT_EQ( |
| 1341 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1347 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1342 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 1348 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1343 | 1349 |
| 1344 imageResource->loader()->cancel(); | 1350 imageResource->loader()->cancel(); |
| 1345 } | 1351 } |
| 1346 | 1352 |
| 1353 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) { |
| 1354 const struct { |
| 1355 int statusCode; |
| 1356 AtomicString contentRange; |
| 1357 } tests[] = { |
| 1358 {200, nullAtom}, |
| 1359 {404, nullAtom}, |
| 1360 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))}, |
| 1361 }; |
| 1362 for (const auto& test : tests) { |
| 1363 KURL testURL(ParsedURLString, kTestURL); |
| 1364 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
| 1365 |
| 1366 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1367 request.setAllowImagePlaceholder(); |
| 1368 ImageResource* imageResource = ImageResource::fetch( |
| 1369 request, |
| 1370 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 1371 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1372 request.placeholderImageRequestType()); |
| 1373 EXPECT_EQ("bytes=0-2047", |
| 1374 imageResource->resourceRequest().httpHeaderField("range")); |
| 1375 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1376 std::unique_ptr<MockImageResourceObserver> observer = |
| 1377 MockImageResourceObserver::create(imageResource->getContent()); |
| 1378 |
| 1379 ResourceResponse response(testURL, "image/jpeg", sizeof(kJpegImage), |
| 1380 nullAtom); |
| 1381 response.setHTTPStatusCode(test.statusCode); |
| 1382 if (test.contentRange != nullAtom) |
| 1383 response.setHTTPHeaderField("content-range", test.contentRange); |
| 1384 imageResource->loader()->didReceiveResponse( |
| 1385 WrappedResourceResponse(response)); |
| 1386 imageResource->loader()->didReceiveData( |
| 1387 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 1388 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 1389 sizeof(kJpegImage)); |
| 1390 |
| 1391 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); |
| 1392 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); |
| 1393 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1394 EXPECT_LT(0, observer->imageChangedCount()); |
| 1395 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 1396 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1397 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 1398 |
| 1399 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1400 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 1401 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 1402 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1403 } |
| 1404 } |
| 1405 |
| 1406 TEST(ImageResourceTest, |
| 1407 FetchAllowPlaceholderFullResponseDecodeFailureNoReload) { |
| 1408 static const char kBadImageData[] = "bad image data"; |
| 1409 |
| 1410 const struct { |
| 1411 int statusCode; |
| 1412 AtomicString contentRange; |
| 1413 size_t dataSize; |
| 1414 } tests[] = { |
| 1415 {200, nullAtom, sizeof(kBadImageData)}, |
| 1416 {206, buildContentRange(sizeof(kBadImageData), sizeof(kBadImageData)), |
| 1417 sizeof(kBadImageData)}, |
| 1418 {204, nullAtom, 0}, |
| 1419 }; |
| 1420 for (const auto& test : tests) { |
| 1421 KURL testURL(ParsedURLString, kTestURL); |
| 1422 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
| 1423 |
| 1424 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1425 request.setAllowImagePlaceholder(); |
| 1426 ImageResource* imageResource = ImageResource::fetch( |
| 1427 request, |
| 1428 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 1429 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1430 request.placeholderImageRequestType()); |
| 1431 EXPECT_EQ("bytes=0-2047", |
| 1432 imageResource->resourceRequest().httpHeaderField("range")); |
| 1433 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1434 std::unique_ptr<MockImageResourceObserver> observer = |
| 1435 MockImageResourceObserver::create(imageResource->getContent()); |
| 1436 |
| 1437 ResourceResponse response(testURL, "image/jpeg", test.dataSize, nullAtom); |
| 1438 response.setHTTPStatusCode(test.statusCode); |
| 1439 if (test.contentRange != nullAtom) |
| 1440 response.setHTTPHeaderField("content-range", test.contentRange); |
| 1441 imageResource->loader()->didReceiveResponse( |
| 1442 WrappedResourceResponse(response)); |
| 1443 imageResource->loader()->didReceiveData(kBadImageData, test.dataSize); |
| 1444 |
| 1445 EXPECT_EQ(ResourceStatus::DecodeError, imageResource->getStatus()); |
| 1446 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1447 } |
| 1448 } |
| 1449 |
| 1450 TEST(ImageResourceTest, |
| 1451 FetchAllowPlaceholderFullResponseDecodeFailureWithReload) { |
| 1452 const int kStatusCodes[] = {404, 500}; |
| 1453 for (int statusCode : kStatusCodes) { |
| 1454 KURL testURL(ParsedURLString, kTestURL); |
| 1455 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
| 1456 |
| 1457 FetchRequest request(testURL, FetchInitiatorInfo()); |
| 1458 request.setAllowImagePlaceholder(); |
| 1459 ImageResource* imageResource = ImageResource::fetch( |
| 1460 request, |
| 1461 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); |
| 1462 EXPECT_EQ(FetchRequest::AllowPlaceholder, |
| 1463 request.placeholderImageRequestType()); |
| 1464 EXPECT_EQ("bytes=0-2047", |
| 1465 imageResource->resourceRequest().httpHeaderField("range")); |
| 1466 EXPECT_TRUE(imageResource->isPlaceholder()); |
| 1467 std::unique_ptr<MockImageResourceObserver> observer = |
| 1468 MockImageResourceObserver::create(imageResource->getContent()); |
| 1469 |
| 1470 static const char kBadImageData[] = "bad image data"; |
| 1471 |
| 1472 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData), |
| 1473 nullAtom); |
| 1474 response.setHTTPStatusCode(statusCode); |
| 1475 imageResource->loader()->didReceiveResponse( |
| 1476 WrappedResourceResponse(response)); |
| 1477 imageResource->loader()->didReceiveData(kBadImageData, |
| 1478 sizeof(kBadImageData)); |
| 1479 |
| 1480 // The dimensions could not be extracted, and the response code was a 4xx |
| 1481 // error, so the full original image should be loading. |
| 1482 EXPECT_EQ(ResourceStatus::Pending, imageResource->getStatus()); |
| 1483 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1484 EXPECT_EQ(nullAtom, |
| 1485 imageResource->resourceRequest().httpHeaderField("range")); |
| 1486 EXPECT_EQ( |
| 1487 static_cast<int>(WebCachePolicy::BypassingCache), |
| 1488 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1489 EXPECT_FALSE(observer->imageNotifyFinishedCalled()); |
| 1490 |
| 1491 imageResource->loader()->didReceiveResponse(WrappedResourceResponse( |
| 1492 ResourceResponse(testURL, "image/jpeg", sizeof(kJpegImage), nullAtom))); |
| 1493 imageResource->loader()->didReceiveData( |
| 1494 reinterpret_cast<const char*>(kJpegImage), sizeof(kJpegImage)); |
| 1495 imageResource->loader()->didFinishLoading(0.0, sizeof(kJpegImage), |
| 1496 sizeof(kJpegImage)); |
| 1497 |
| 1498 EXPECT_EQ(ResourceStatus::Cached, imageResource->getStatus()); |
| 1499 EXPECT_EQ(sizeof(kJpegImage), imageResource->encodedSize()); |
| 1500 EXPECT_FALSE(imageResource->isPlaceholder()); |
| 1501 EXPECT_LT(0, observer->imageChangedCount()); |
| 1502 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnLastImageChanged()); |
| 1503 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1504 EXPECT_EQ(kJpegImageWidth, observer->imageWidthOnImageNotifyFinished()); |
| 1505 |
| 1506 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1507 EXPECT_EQ(1, imageResource->getContent()->getImage()->width()); |
| 1508 EXPECT_EQ(1, imageResource->getContent()->getImage()->height()); |
| 1509 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1510 } |
| 1511 } |
| 1512 |
| 1347 TEST(ImageResourceTest, PeriodicFlushTest) { | 1513 TEST(ImageResourceTest, PeriodicFlushTest) { |
| 1348 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> | 1514 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 1349 platform; | 1515 platform; |
| 1350 KURL testURL(ParsedURLString, kTestURL); | 1516 KURL testURL(ParsedURLString, kTestURL); |
| 1351 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); | 1517 ScopedMockedURLLoad scopedMockedURLLoad(testURL, GetTestFilePath()); |
| 1352 ResourceRequest request = ResourceRequest(testURL); | 1518 ResourceRequest request = ResourceRequest(testURL); |
| 1353 ImageResource* imageResource = ImageResource::create(request); | 1519 ImageResource* imageResource = ImageResource::create(request); |
| 1354 imageResource->setStatus(ResourceStatus::Pending); | 1520 imageResource->setStatus(ResourceStatus::Pending); |
| 1355 | 1521 |
| 1356 std::unique_ptr<MockImageResourceObserver> observer = | 1522 std::unique_ptr<MockImageResourceObserver> observer = |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1425 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); | 1591 EXPECT_TRUE(observer->imageNotifyFinishedCalled()); |
| 1426 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1592 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1427 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 1593 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 1428 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 1594 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 1429 | 1595 |
| 1430 WTF::setTimeFunctionsForTesting(nullptr); | 1596 WTF::setTimeFunctionsForTesting(nullptr); |
| 1431 } | 1597 } |
| 1432 | 1598 |
| 1433 } // namespace | 1599 } // namespace |
| 1434 } // namespace blink | 1600 } // namespace blink |
| OLD | NEW |