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

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

Issue 2543073002: Don't show a placeholder if the response contains the full image. (Closed)
Patch Set: Initial patchset Created 4 years 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
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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 205
206 ~ScopedRegisteredURL() { 206 ~ScopedRegisteredURL() {
207 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url); 207 Platform::current()->getURLLoaderMockFactory()->unregisterURL(m_url);
208 } 208 }
209 209
210 private: 210 private:
211 KURL m_url; 211 KURL m_url;
212 }; 212 };
213 213
214 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) { 214 AtomicString buildContentRange(size_t rangeLength, size_t totalLength) {
215 return AtomicString(String("bytes 0-" + String::number(rangeLength) + "/" + 215 return AtomicString(String("bytes 0-" + String::number(rangeLength - 1) +
216 String::number(totalLength))); 216 "/" + String::number(totalLength)));
217 } 217 }
218 218
219 TEST(ImageResourceTest, MultipartImage) { 219 TEST(ImageResourceTest, MultipartImage) {
220 ResourceFetcher* fetcher = 220 ResourceFetcher* fetcher =
221 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()); 221 ResourceFetcher::create(ImageResourceTestMockFetchContext::create());
222 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 222 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
223 ScopedRegisteredURL scopedRegisteredURL(testURL); 223 ScopedRegisteredURL scopedRegisteredURL(testURL);
224 224
225 // Emulate starting a real load, but don't expect any "real" 225 // Emulate starting a real load, but don't expect any "real"
226 // WebURLLoaderClient callbacks. 226 // WebURLLoaderClient callbacks.
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 ResourceFetcher::create(ImageResourceTestMockFetchContext::create())); 1013 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1014 EXPECT_EQ(FetchRequest::AllowPlaceholder, 1014 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1015 request.placeholderImageRequestType()); 1015 request.placeholderImageRequestType());
1016 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range")); 1016 EXPECT_EQ("bytes=0-2047", image->resourceRequest().httpHeaderField("range"));
1017 EXPECT_TRUE(image->isPlaceholder()); 1017 EXPECT_TRUE(image->isPlaceholder());
1018 Persistent<MockImageResourceClient> client = 1018 Persistent<MockImageResourceClient> client =
1019 new MockImageResourceClient(image); 1019 new MockImageResourceClient(image);
1020 1020
1021 const char kBadData[] = "notanimageresponse"; 1021 const char kBadData[] = "notanimageresponse";
1022 1022
1023 image->loader()->didReceiveResponse( 1023 ResourceResponse badResponse(testURL, "image/jpeg", sizeof(kBadData),
1024 WrappedResourceResponse(ResourceResponse( 1024 nullAtom, String());
1025 testURL, "image/jpeg", sizeof(kBadData), nullAtom, String()))); 1025 badResponse.setHTTPStatusCode(206);
1026 badResponse.setHTTPHeaderField(
1027 "content-range", buildContentRange(sizeof(kBadData), sizeof(kJpegImage)));
1028
1029 image->loader()->didReceiveResponse(WrappedResourceResponse(badResponse));
1026 image->loader()->didReceiveData(kBadData, sizeof(kBadData), sizeof(kBadData)); 1030 image->loader()->didReceiveData(kBadData, sizeof(kBadData), sizeof(kBadData));
1027 1031
1028 // The dimensions could not be extracted, so the full original image should be 1032 // The dimensions could not be extracted, so the full original image should be
1029 // loading. 1033 // loading.
1030 EXPECT_EQ(Resource::Pending, image->getStatus()); 1034 EXPECT_EQ(Resource::Pending, image->getStatus());
1031 EXPECT_FALSE(image->isPlaceholder()); 1035 EXPECT_FALSE(image->isPlaceholder());
1032 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1036 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
1033 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache), 1037 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache),
1034 static_cast<int>(image->resourceRequest().getCachePolicy())); 1038 static_cast<int>(image->resourceRequest().getCachePolicy()));
1035 EXPECT_FALSE(client->notifyFinishedCalled()); 1039 EXPECT_FALSE(client->notifyFinishedCalled());
1036 EXPECT_EQ(0, client->imageNotifyFinishedCount()); 1040 EXPECT_EQ(0, client->imageNotifyFinishedCount());
1037 1041
1038 image->loader()->didReceiveResponse( 1042 image->loader()->didReceiveResponse(WrappedResourceResponse(ResourceResponse(
1039 WrappedResourceResponse(ResourceResponse( 1043 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1040 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1041 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage), 1044 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
1042 sizeof(kJpegImage), sizeof(kJpegImage)); 1045 sizeof(kJpegImage), sizeof(kJpegImage));
1043 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage), 1046 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1044 sizeof(kJpegImage)); 1047 sizeof(kJpegImage));
1045 1048
1046 EXPECT_EQ(Resource::Cached, image->getStatus()); 1049 EXPECT_EQ(Resource::Cached, image->getStatus());
1047 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize()); 1050 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize());
1048 EXPECT_FALSE(image->isPlaceholder()); 1051 EXPECT_FALSE(image->isPlaceholder());
1049 EXPECT_LT(0, client->imageChangedCount()); 1052 EXPECT_LT(0, client->imageChangedCount());
1050 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged()); 1053 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1124 EXPECT_EQ(image, secondImage); 1127 EXPECT_EQ(image, secondImage);
1125 EXPECT_EQ(Resource::Pending, image->getStatus()); 1128 EXPECT_EQ(Resource::Pending, image->getStatus());
1126 EXPECT_FALSE(image->isPlaceholder()); 1129 EXPECT_FALSE(image->isPlaceholder());
1127 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); 1130 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
1128 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), 1131 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy),
1129 static_cast<int>(image->resourceRequest().getCachePolicy())); 1132 static_cast<int>(image->resourceRequest().getCachePolicy()));
1130 1133
1131 image->loader()->cancel(); 1134 image->loader()->cancel();
1132 } 1135 }
1133 1136
1137 TEST(ImageResourceTest, FetchAllowPlaceholderFullResponseDecodeSuccess) {
1138 const struct {
1139 int statusCode;
1140 AtomicString contentRange;
1141 } tests[] = {
1142 {200, nullAtom},
1143 {404, nullAtom},
1144 {206, buildContentRange(sizeof(kJpegImage), sizeof(kJpegImage))},
1145 };
1146 for (const auto& test : tests) {
1147 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1148 ScopedRegisteredURL scopedRegisteredURL(testURL);
1149
1150 FetchRequest request(testURL, FetchInitiatorInfo());
1151 request.setAllowImagePlaceholder();
1152 ImageResource* image = ImageResource::fetch(
1153 request,
1154 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1155 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1156 request.placeholderImageRequestType());
1157 EXPECT_EQ("bytes=0-2047",
1158 image->resourceRequest().httpHeaderField("range"));
1159 EXPECT_TRUE(image->isPlaceholder());
1160 Persistent<MockImageResourceClient> client =
1161 new MockImageResourceClient(image);
1162
1163 ResourceResponse response(testURL, "image/jpeg", sizeof(kJpegImage),
1164 nullAtom, String());
1165 response.setHTTPStatusCode(test.statusCode);
1166 if (test.contentRange != nullAtom)
1167 response.setHTTPHeaderField("content-range", test.contentRange);
1168 image->loader()->didReceiveResponse(WrappedResourceResponse(response));
1169 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
1170 sizeof(kJpegImage), sizeof(kJpegImage));
1171 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1172 sizeof(kJpegImage));
1173
1174 EXPECT_EQ(Resource::Cached, image->getStatus());
1175 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize());
1176 EXPECT_FALSE(image->isPlaceholder());
1177 EXPECT_LT(0, client->imageChangedCount());
1178 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
1179 EXPECT_TRUE(client->notifyFinishedCalled());
1180 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
1181 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
1182
1183 ASSERT_TRUE(image->hasImage());
1184 EXPECT_EQ(1, image->getImage()->width());
1185 EXPECT_EQ(1, image->getImage()->height());
1186 EXPECT_TRUE(image->getImage()->isBitmapImage());
1187 }
1188 }
1189
1190 TEST(ImageResourceTest,
1191 FetchAllowPlaceholderFullResponseDecodeFailureNoReload) {
1192 static const char kBadImageData[] = "bad image data";
1193
1194 const struct {
1195 int statusCode;
1196 AtomicString contentRange;
1197 size_t dataSize;
1198 } tests[] = {
1199 {200, nullAtom, sizeof(kBadImageData)},
1200 {206, buildContentRange(sizeof(kBadImageData), sizeof(kBadImageData)),
1201 sizeof(kBadImageData)},
1202 {204, nullAtom, 0},
1203 };
1204 for (const auto& test : tests) {
1205 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1206 ScopedRegisteredURL scopedRegisteredURL(testURL);
1207
1208 FetchRequest request(testURL, FetchInitiatorInfo());
1209 request.setAllowImagePlaceholder();
1210 ImageResource* image = ImageResource::fetch(
1211 request,
1212 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1213 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1214 request.placeholderImageRequestType());
1215 EXPECT_EQ("bytes=0-2047",
1216 image->resourceRequest().httpHeaderField("range"));
1217 EXPECT_TRUE(image->isPlaceholder());
1218 Persistent<MockImageResourceClient> client =
1219 new MockImageResourceClient(image);
1220
1221 ResourceResponse response(testURL, "image/jpeg", test.dataSize, nullAtom,
1222 String());
1223 response.setHTTPStatusCode(test.statusCode);
1224 if (test.contentRange != nullAtom)
1225 response.setHTTPHeaderField("content-range", test.contentRange);
1226 image->loader()->didReceiveResponse(WrappedResourceResponse(response));
1227 image->loader()->didReceiveData(kBadImageData, test.dataSize,
1228 test.dataSize);
1229
1230 EXPECT_EQ(Resource::DecodeError, image->getStatus());
1231 EXPECT_FALSE(image->isPlaceholder());
1232 }
1233 }
1234
1235 TEST(ImageResourceTest,
1236 FetchAllowPlaceholderFullResponseDecodeFailureWithReload) {
1237 const int kStatusCodes[] = {404, 500};
1238 for (int statusCode : kStatusCodes) {
1239 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1240 ScopedRegisteredURL scopedRegisteredURL(testURL);
1241
1242 FetchRequest request(testURL, FetchInitiatorInfo());
1243 request.setAllowImagePlaceholder();
1244 ImageResource* image = ImageResource::fetch(
1245 request,
1246 ResourceFetcher::create(ImageResourceTestMockFetchContext::create()));
1247 EXPECT_EQ(FetchRequest::AllowPlaceholder,
1248 request.placeholderImageRequestType());
1249 EXPECT_EQ("bytes=0-2047",
1250 image->resourceRequest().httpHeaderField("range"));
1251 EXPECT_TRUE(image->isPlaceholder());
1252 Persistent<MockImageResourceClient> client =
1253 new MockImageResourceClient(image);
1254
1255 static const char kBadImageData[] = "bad image data";
1256
1257 ResourceResponse response(testURL, "image/jpeg", sizeof(kBadImageData),
1258 nullAtom, String());
1259 response.setHTTPStatusCode(statusCode);
1260 image->loader()->didReceiveResponse(WrappedResourceResponse(response));
1261 image->loader()->didReceiveData(kBadImageData, sizeof(kBadImageData),
1262 sizeof(kBadImageData));
1263
1264 // The dimensions could not be extracted, and the response code was a 4xx
1265 // error, so the full original image should be loading.
1266 EXPECT_EQ(Resource::Pending, image->getStatus());
1267 EXPECT_FALSE(image->isPlaceholder());
1268 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range"));
1269 EXPECT_EQ(static_cast<int>(WebCachePolicy::BypassingCache),
1270 static_cast<int>(image->resourceRequest().getCachePolicy()));
1271 EXPECT_FALSE(client->notifyFinishedCalled());
1272
1273 image->loader()->didReceiveResponse(
1274
1275 WrappedResourceResponse(ResourceResponse(
1276 testURL, "image/jpeg", sizeof(kJpegImage), nullAtom, String())));
1277 image->loader()->didReceiveData(reinterpret_cast<const char*>(kJpegImage),
1278 sizeof(kJpegImage), sizeof(kJpegImage));
1279 image->loader()->didFinishLoading(0.0, sizeof(kJpegImage),
1280 sizeof(kJpegImage));
1281
1282 EXPECT_EQ(Resource::Cached, image->getStatus());
1283 EXPECT_EQ(sizeof(kJpegImage), image->encodedSize());
1284 EXPECT_FALSE(image->isPlaceholder());
1285 EXPECT_LT(0, client->imageChangedCount());
1286 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnLastImageChanged());
1287 EXPECT_TRUE(client->notifyFinishedCalled());
1288 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnNotifyFinished());
1289 EXPECT_EQ(sizeof(kJpegImage), client->encodedSizeOnImageNotifyFinished());
1290
1291 ASSERT_TRUE(image->hasImage());
1292 EXPECT_EQ(1, image->getImage()->width());
1293 EXPECT_EQ(1, image->getImage()->height());
1294 EXPECT_TRUE(image->getImage()->isBitmapImage());
1295 }
1296 }
1297
1134 TEST(ImageResourceTest, PeriodicFlushTest) { 1298 TEST(ImageResourceTest, PeriodicFlushTest) {
1135 TestingPlatformSupportWithMockScheduler platform; 1299 TestingPlatformSupportWithMockScheduler platform;
1136 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); 1300 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
1137 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", 1301 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html",
1138 "text/html"); 1302 "text/html");
1139 ResourceRequest request = ResourceRequest(testURL); 1303 ResourceRequest request = ResourceRequest(testURL);
1140 ImageResource* cachedImage = ImageResource::create(request); 1304 ImageResource* cachedImage = ImageResource::create(request);
1141 cachedImage->setStatus(Resource::Pending); 1305 cachedImage->setStatus(Resource::Pending);
1142 1306
1143 Persistent<MockImageResourceClient> client = 1307 Persistent<MockImageResourceClient> client =
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 EXPECT_TRUE(client->notifyFinishedCalled()); 1376 EXPECT_TRUE(client->notifyFinishedCalled());
1213 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); 1377 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage());
1214 EXPECT_EQ(50, cachedImage->getImage()->width()); 1378 EXPECT_EQ(50, cachedImage->getImage()->width());
1215 EXPECT_EQ(50, cachedImage->getImage()->height()); 1379 EXPECT_EQ(50, cachedImage->getImage()->height());
1216 1380
1217 WTF::setTimeFunctionsForTesting(nullptr); 1381 WTF::setTimeFunctionsForTesting(nullptr);
1218 } 1382 }
1219 1383
1220 } // namespace 1384 } // namespace
1221 } // namespace blink 1385 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698