| 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 24 matching lines...) Expand all Loading... |
| 35 #include "core/fetch/MemoryCache.h" | 35 #include "core/fetch/MemoryCache.h" |
| 36 #include "core/fetch/MockResourceClients.h" | 36 #include "core/fetch/MockResourceClients.h" |
| 37 #include "core/fetch/ResourceFetcher.h" | 37 #include "core/fetch/ResourceFetcher.h" |
| 38 #include "core/fetch/ResourceLoader.h" | 38 #include "core/fetch/ResourceLoader.h" |
| 39 #include "core/fetch/UniqueIdentifier.h" | 39 #include "core/fetch/UniqueIdentifier.h" |
| 40 #include "platform/SharedBuffer.h" | 40 #include "platform/SharedBuffer.h" |
| 41 #include "platform/exported/WrappedResourceResponse.h" | 41 #include "platform/exported/WrappedResourceResponse.h" |
| 42 #include "platform/graphics/BitmapImage.h" | 42 #include "platform/graphics/BitmapImage.h" |
| 43 #include "platform/graphics/Image.h" | 43 #include "platform/graphics/Image.h" |
| 44 #include "platform/scheduler/test/fake_web_task_runner.h" | 44 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 45 #include "platform/testing/TestingPlatformSupport.h" |
| 45 #include "platform/testing/URLTestHelpers.h" | 46 #include "platform/testing/URLTestHelpers.h" |
| 46 #include "platform/testing/UnitTestHelpers.h" | 47 #include "platform/testing/UnitTestHelpers.h" |
| 47 #include "public/platform/Platform.h" | 48 #include "public/platform/Platform.h" |
| 48 #include "public/platform/WebCachePolicy.h" | 49 #include "public/platform/WebCachePolicy.h" |
| 49 #include "public/platform/WebURL.h" | 50 #include "public/platform/WebURL.h" |
| 50 #include "public/platform/WebURLLoaderMockFactory.h" | 51 #include "public/platform/WebURLLoaderMockFactory.h" |
| 51 #include "public/platform/WebURLResponse.h" | 52 #include "public/platform/WebURLResponse.h" |
| 52 #include "testing/gtest/include/gtest/gtest.h" | 53 #include "testing/gtest/include/gtest/gtest.h" |
| 53 #include "wtf/PtrUtil.h" | 54 #include "wtf/PtrUtil.h" |
| 54 #include "wtf/text/Base64.h" | 55 #include "wtf/text/Base64.h" |
| (...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 EXPECT_EQ(image, secondImage); | 1119 EXPECT_EQ(image, secondImage); |
| 1119 EXPECT_EQ(Resource::Pending, image->getStatus()); | 1120 EXPECT_EQ(Resource::Pending, image->getStatus()); |
| 1120 EXPECT_FALSE(image->isPlaceholder()); | 1121 EXPECT_FALSE(image->isPlaceholder()); |
| 1121 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); | 1122 EXPECT_EQ(nullAtom, image->resourceRequest().httpHeaderField("range")); |
| 1122 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), | 1123 EXPECT_EQ(static_cast<int>(WebCachePolicy::UseProtocolCachePolicy), |
| 1123 static_cast<int>(image->resourceRequest().getCachePolicy())); | 1124 static_cast<int>(image->resourceRequest().getCachePolicy())); |
| 1124 | 1125 |
| 1125 image->loader()->cancel(); | 1126 image->loader()->cancel(); |
| 1126 } | 1127 } |
| 1127 | 1128 |
| 1129 TEST(ImageResourceTest, PeriodicFlushTest) { |
| 1130 TestingPlatformSupportWithMockScheduler platform; |
| 1131 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 1132 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", |
| 1133 "text/html"); |
| 1134 ResourceRequest request = ResourceRequest(testURL); |
| 1135 ImageResource* cachedImage = ImageResource::create(request); |
| 1136 cachedImage->setStatus(Resource::Pending); |
| 1137 |
| 1138 Persistent<MockImageResourceClient> client = |
| 1139 new MockImageResourceClient(cachedImage); |
| 1140 |
| 1141 // Send the image response. |
| 1142 ResourceResponse resourceResponse(KURL(), "image/jpeg", sizeof(kJpegImage2), |
| 1143 nullAtom, String()); |
| 1144 resourceResponse.addHTTPHeaderField("chrome-proxy", "q=low"); |
| 1145 |
| 1146 cachedImage->responseReceived(resourceResponse, nullptr); |
| 1147 |
| 1148 // This is number is sufficiently large amount of bytes necessary for the |
| 1149 // image to be created (since the size is known). This was determined by |
| 1150 // appending one byte at a time (with flushes) until the image was decoded. |
| 1151 size_t meaningfulImageSize = 280; |
| 1152 cachedImage->appendData(reinterpret_cast<const char*>(kJpegImage2), |
| 1153 meaningfulImageSize); |
| 1154 size_t bytesSent = meaningfulImageSize; |
| 1155 |
| 1156 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 1157 EXPECT_TRUE(cachedImage->hasImage()); |
| 1158 EXPECT_EQ(1, client->imageChangedCount()); |
| 1159 |
| 1160 platform.runForPeriodSeconds(1.); |
| 1161 platform.advanceClockSeconds(1.); |
| 1162 |
| 1163 // Sanity check that we created an image after appending |meaningfulImageSize| |
| 1164 // bytes just once. |
| 1165 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 1166 ASSERT_TRUE(cachedImage->hasImage()); |
| 1167 EXPECT_EQ(1, client->imageChangedCount()); |
| 1168 |
| 1169 for (int flushCount = 1; flushCount <= 3; ++flushCount) { |
| 1170 // For each of the iteration that appends data, we don't expect |
| 1171 // |imageChangeCount()| to change, since the time is adjusted by 0.2001 |
| 1172 // seconds (it's greater than 0.2 to avoid double precision problems). |
| 1173 // After 5 appends, we breach the flush interval and the flush count |
| 1174 // increases. |
| 1175 for (int i = 0; i < 5; ++i) { |
| 1176 SCOPED_TRACE(i); |
| 1177 cachedImage->appendData( |
| 1178 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, 1); |
| 1179 |
| 1180 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 1181 ASSERT_TRUE(cachedImage->hasImage()); |
| 1182 EXPECT_EQ(flushCount, client->imageChangedCount()); |
| 1183 |
| 1184 ++bytesSent; |
| 1185 platform.runForPeriodSeconds(0.2001); |
| 1186 } |
| 1187 } |
| 1188 |
| 1189 // Increasing time by a large number only causes one extra flush. |
| 1190 platform.runForPeriodSeconds(10.); |
| 1191 platform.advanceClockSeconds(10.); |
| 1192 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 1193 ASSERT_TRUE(cachedImage->hasImage()); |
| 1194 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 1195 EXPECT_EQ(4, client->imageChangedCount()); |
| 1196 |
| 1197 // Append the rest of the data and finish (which causes another flush). |
| 1198 cachedImage->appendData( |
| 1199 reinterpret_cast<const char*>(kJpegImage2) + bytesSent, |
| 1200 sizeof(kJpegImage2) - bytesSent); |
| 1201 cachedImage->finish(); |
| 1202 |
| 1203 EXPECT_FALSE(cachedImage->errorOccurred()); |
| 1204 ASSERT_TRUE(cachedImage->hasImage()); |
| 1205 EXPECT_FALSE(cachedImage->getImage()->isNull()); |
| 1206 EXPECT_EQ(5, client->imageChangedCount()); |
| 1207 EXPECT_TRUE(client->notifyFinishedCalled()); |
| 1208 EXPECT_TRUE(cachedImage->getImage()->isBitmapImage()); |
| 1209 EXPECT_EQ(50, cachedImage->getImage()->width()); |
| 1210 EXPECT_EQ(50, cachedImage->getImage()->height()); |
| 1211 |
| 1212 WTF::setTimeFunctionsForTesting(nullptr); |
| 1213 } |
| 1214 |
| 1128 } // namespace | 1215 } // namespace |
| 1129 | |
| 1130 } // namespace blink | 1216 } // namespace blink |
| OLD | NEW |