| 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 1143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 EXPECT_EQ(nullAtom, | 1154 EXPECT_EQ(nullAtom, |
| 1155 imageResource->resourceRequest().httpHeaderField("range")); | 1155 imageResource->resourceRequest().httpHeaderField("range")); |
| 1156 EXPECT_EQ( | 1156 EXPECT_EQ( |
| 1157 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1157 static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1158 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); | 1158 static_cast<int>(imageResource->resourceRequest().getCachePolicy())); |
| 1159 | 1159 |
| 1160 imageResource->loader()->cancel(); | 1160 imageResource->loader()->cancel(); |
| 1161 } | 1161 } |
| 1162 | 1162 |
| 1163 TEST(ImageResourceTest, PeriodicFlushTest) { | 1163 TEST(ImageResourceTest, PeriodicFlushTest) { |
| 1164 TestingPlatformSupportWithMockScheduler platform; | 1164 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> |
| 1165 platform(WTF::makeUnique<TestingPlatformSupportWithMockScheduler>()); |
| 1165 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 1166 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1166 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", | 1167 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", |
| 1167 "text/html"); | 1168 "text/html"); |
| 1168 ResourceRequest request = ResourceRequest(testURL); | 1169 ResourceRequest request = ResourceRequest(testURL); |
| 1169 ImageResource* imageResource = ImageResource::create(request); | 1170 ImageResource* imageResource = ImageResource::create(request); |
| 1170 imageResource->setStatus(Resource::Pending); | 1171 imageResource->setStatus(Resource::Pending); |
| 1171 | 1172 |
| 1172 Persistent<MockImageResourceClient> client = | 1173 Persistent<MockImageResourceClient> client = |
| 1173 new MockImageResourceClient(imageResource); | 1174 new MockImageResourceClient(imageResource); |
| 1174 | 1175 |
| 1175 // Send the image response. | 1176 // Send the image response. |
| 1176 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), | 1177 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), |
| 1177 nullAtom, String()); | 1178 nullAtom, String()); |
| 1178 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); | 1179 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 1179 | 1180 |
| 1180 imageResource->responseReceived(resourceResponse, nullptr); | 1181 imageResource->responseReceived(resourceResponse, nullptr); |
| 1181 | 1182 |
| 1182 // This is number is sufficiently large amount of bytes necessary for the | 1183 // This is number is sufficiently large amount of bytes necessary for the |
| 1183 // image to be created (since the size is known). This was determined by | 1184 // image to be created (since the size is known). This was determined by |
| 1184 // appending one byte at a time (with flushes) until the image was decoded. | 1185 // appending one byte at a time (with flushes) until the image was decoded. |
| 1185 size_t meaningfulImageSize = 280; | 1186 size_t meaningfulImageSize = 280; |
| 1186 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage2), | 1187 imageResource->appendData(reinterpret_cast<const char*>(kJpegImage2), |
| 1187 meaningfulImageSize); | 1188 meaningfulImageSize); |
| 1188 size_t bytesSent = meaningfulImageSize; | 1189 size_t bytesSent = meaningfulImageSize; |
| 1189 | 1190 |
| 1190 EXPECT_FALSE(imageResource->errorOccurred()); | 1191 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1191 EXPECT_TRUE(imageResource->getContent()->hasImage()); | 1192 EXPECT_TRUE(imageResource->getContent()->hasImage()); |
| 1192 EXPECT_EQ(1, client->imageChangedCount()); | 1193 EXPECT_EQ(1, client->imageChangedCount()); |
| 1193 | 1194 |
| 1194 platform.runForPeriodSeconds(1.); | 1195 platform->runForPeriodSeconds(1.); |
| 1195 platform.advanceClockSeconds(1.); | 1196 platform->advanceClockSeconds(1.); |
| 1196 | 1197 |
| 1197 // Sanity check that we created an image after appending |meaningfulImageSize| | 1198 // Sanity check that we created an image after appending |meaningfulImageSize| |
| 1198 // bytes just once. | 1199 // bytes just once. |
| 1199 EXPECT_FALSE(imageResource->errorOccurred()); | 1200 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1200 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1201 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1201 EXPECT_EQ(1, client->imageChangedCount()); | 1202 EXPECT_EQ(1, client->imageChangedCount()); |
| 1202 | 1203 |
| 1203 for (int flushCount = 1; flushCount <= 3; ++flushCount) { | 1204 for (int flushCount = 1; flushCount <= 3; ++flushCount) { |
| 1204 // For each of the iteration that appends data, we don't expect | 1205 // For each of the iteration that appends data, we don't expect |
| 1205 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 | 1206 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 |
| 1206 // seconds (it's greater than 0.2 to avoid double precision problems). | 1207 // seconds (it's greater than 0.2 to avoid double precision problems). |
| 1207 // After 5 appends, we breach the flush interval and the flush count | 1208 // After 5 appends, we breach the flush interval and the flush count |
| 1208 // increases. | 1209 // increases. |
| 1209 for (int i = 0; i < 5; ++i) { | 1210 for (int i = 0; i < 5; ++i) { |
| 1210 SCOPED_TRACE(i); | 1211 SCOPED_TRACE(i); |
| 1211 imageResource->appendData( | 1212 imageResource->appendData( |
| 1212 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); | 1213 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); |
| 1213 | 1214 |
| 1214 EXPECT_FALSE(imageResource->errorOccurred()); | 1215 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1215 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1216 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1216 EXPECT_EQ(flushCount, client->imageChangedCount()); | 1217 EXPECT_EQ(flushCount, client->imageChangedCount()); |
| 1217 | 1218 |
| 1218 ++bytesSent; | 1219 ++bytesSent; |
| 1219 platform.runForPeriodSeconds(0.2001); | 1220 platform->runForPeriodSeconds(0.2001); |
| 1220 } | 1221 } |
| 1221 } | 1222 } |
| 1222 | 1223 |
| 1223 // Increasing time by a large number only causes one extra flush. | 1224 // Increasing time by a large number only causes one extra flush. |
| 1224 platform.runForPeriodSeconds(10.); | 1225 platform->runForPeriodSeconds(10.); |
| 1225 platform.advanceClockSeconds(10.); | 1226 platform->advanceClockSeconds(10.); |
| 1226 EXPECT_FALSE(imageResource->errorOccurred()); | 1227 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1227 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1228 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1228 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 1229 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 1229 EXPECT_EQ(4, client->imageChangedCount()); | 1230 EXPECT_EQ(4, client->imageChangedCount()); |
| 1230 | 1231 |
| 1231 // Append the rest of the data and finish (which causes another flush). | 1232 // Append the rest of the data and finish (which causes another flush). |
| 1232 imageResource->appendData( | 1233 imageResource->appendData( |
| 1233 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, | 1234 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, |
| 1234 sizeof(kJpegImage2) - bytesSent); | 1235 sizeof(kJpegImage2) - bytesSent); |
| 1235 imageResource->finish(); | 1236 imageResource->finish(); |
| 1236 | 1237 |
| 1237 EXPECT_FALSE(imageResource->errorOccurred()); | 1238 EXPECT_FALSE(imageResource->errorOccurred()); |
| 1238 ASSERT_TRUE(imageResource->getContent()->hasImage()); | 1239 ASSERT_TRUE(imageResource->getContent()->hasImage()); |
| 1239 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); | 1240 EXPECT_FALSE(imageResource->getContent()->getImage()->isNull()); |
| 1240 EXPECT_EQ(5, client->imageChangedCount()); | 1241 EXPECT_EQ(5, client->imageChangedCount()); |
| 1241 EXPECT_TRUE(client->notifyFinishedCalled()); | 1242 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1242 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); | 1243 EXPECT_TRUE(imageResource->getContent()->getImage()->isBitmapImage()); |
| 1243 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); | 1244 EXPECT_EQ(50, imageResource->getContent()->getImage()->width()); |
| 1244 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); | 1245 EXPECT_EQ(50, imageResource->getContent()->getImage()->height()); |
| 1245 | 1246 |
| 1246 WTF::setTimeFunctionsForTesting(nullptr); | 1247 WTF::setTimeFunctionsForTesting(nullptr); |
| 1247 } | 1248 } |
| 1248 | 1249 |
| 1249 } // namespace | 1250 } // namespace |
| 1250 } // namespace blink | 1251 } // namespace blink |
| OLD | NEW |