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