| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 return jpeg; | 82 return jpeg; |
| 83 } | 83 } |
| 84 | 84 |
| 85 TEST(ImageResourceTest, MultipartImage) | 85 TEST(ImageResourceTest, MultipartImage) |
| 86 { | 86 { |
| 87 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); | 87 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); |
| 88 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 88 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 89 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); | 89 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); |
| 90 | 90 |
| 91 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli
ent callbacks. | 91 // Emulate starting a real load, but don't expect any "real" WebURLLoaderCli
ent callbacks. |
| 92 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL)); | 92 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); |
| 93 cachedImage->setIdentifier(createUniqueIdentifier()); | 93 cachedImage->setIdentifier(createUniqueIdentifier()); |
| 94 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); | 94 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); |
| 95 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | 95 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); |
| 96 | 96 |
| 97 MockImageResourceClient client; | 97 MockImageResourceClient client; |
| 98 cachedImage->addClient(&client); | 98 cachedImage->addClient(&client); |
| 99 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 99 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
| 100 | 100 |
| 101 // Send the multipart response. No image or data buffer is created. | 101 // Send the multipart response. No image or data buffer is created. |
| 102 // Note that the response must be routed through ResourceLoader to | 102 // Note that the response must be routed through ResourceLoader to |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } | 139 } |
| 140 | 140 |
| 141 TEST(ImageResourceTest, CancelOnDetach) | 141 TEST(ImageResourceTest, CancelOnDetach) |
| 142 { | 142 { |
| 143 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); | 143 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html"); |
| 144 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); | 144 URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html", "text/html
"); |
| 145 | 145 |
| 146 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); | 146 RefPtrWillBeRawPtr<ResourceFetcher> fetcher = ResourceFetcher::create(nullpt
r); |
| 147 | 147 |
| 148 // Emulate starting a real load. | 148 // Emulate starting a real load. |
| 149 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL)); | 149 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(t
estURL), nullptr); |
| 150 cachedImage->setIdentifier(createUniqueIdentifier()); | 150 cachedImage->setIdentifier(createUniqueIdentifier()); |
| 151 | 151 |
| 152 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); | 152 cachedImage->load(fetcher.get(), ResourceLoaderOptions()); |
| 153 memoryCache()->add(cachedImage.get()); | 153 memoryCache()->add(cachedImage.get()); |
| 154 | 154 |
| 155 MockImageResourceClient client; | 155 MockImageResourceClient client; |
| 156 cachedImage->addClient(&client); | 156 cachedImage->addClient(&client); |
| 157 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 157 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
| 158 | 158 |
| 159 // The load should still be alive, but a timer should be started to cancel t
he load inside removeClient(). | 159 // The load should still be alive, but a timer should be started to cancel t
he load inside removeClient(). |
| 160 cachedImage->removeClient(&client); | 160 cachedImage->removeClient(&client); |
| 161 EXPECT_EQ(Resource::Pending, cachedImage->status()); | 161 EXPECT_EQ(Resource::Pending, cachedImage->status()); |
| 162 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); | 162 EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); |
| 163 | 163 |
| 164 // Trigger the cancel timer, ensure the load was cancelled and the resource
was evicted from the cache. | 164 // Trigger the cancel timer, ensure the load was cancelled and the resource
was evicted from the cache. |
| 165 blink::testing::runPendingTasks(); | 165 blink::testing::runPendingTasks(); |
| 166 EXPECT_EQ(Resource::LoadError, cachedImage->status()); | 166 EXPECT_EQ(Resource::LoadError, cachedImage->status()); |
| 167 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); | 167 EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(test
URL)); |
| 168 | 168 |
| 169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); | 169 Platform::current()->unitTestSupport()->unregisterMockedURL(testURL); |
| 170 } | 170 } |
| 171 | 171 |
| 172 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) | 172 TEST(ImageResourceTest, DecodedDataRemainsWhileHasClients) |
| 173 { | 173 { |
| 174 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); | 174 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); |
| 175 cachedImage->setLoading(true); | 175 cachedImage->setLoading(true); |
| 176 | 176 |
| 177 MockImageResourceClient client; | 177 MockImageResourceClient client; |
| 178 cachedImage->addClient(&client); | 178 cachedImage->addClient(&client); |
| 179 | 179 |
| 180 // Send the image response. | 180 // Send the image response. |
| 181 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String()), nullptr); | 181 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re
place", 0, nullAtom, String()), nullptr); |
| 182 | 182 |
| 183 Vector<unsigned char> jpeg = jpegImage(); | 183 Vector<unsigned char> jpeg = jpegImage(); |
| 184 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | 184 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 198 // The ImageResource no longer has clients. The image should be deleted by p
rune. | 198 // The ImageResource no longer has clients. The image should be deleted by p
rune. |
| 199 cachedImage->removeClient(&client); | 199 cachedImage->removeClient(&client); |
| 200 cachedImage->prune(); | 200 cachedImage->prune(); |
| 201 ASSERT_FALSE(cachedImage->hasClients()); | 201 ASSERT_FALSE(cachedImage->hasClients()); |
| 202 ASSERT_FALSE(cachedImage->hasImage()); | 202 ASSERT_FALSE(cachedImage->hasImage()); |
| 203 ASSERT_TRUE(cachedImage->image()->isNull()); | 203 ASSERT_TRUE(cachedImage->image()->isNull()); |
| 204 } | 204 } |
| 205 | 205 |
| 206 TEST(ImageResourceTest, UpdateBitmapImages) | 206 TEST(ImageResourceTest, UpdateBitmapImages) |
| 207 { | 207 { |
| 208 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
); | 208 ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest()
, nullptr); |
| 209 cachedImage->setLoading(true); | 209 cachedImage->setLoading(true); |
| 210 | 210 |
| 211 MockImageResourceClient client; | 211 MockImageResourceClient client; |
| 212 cachedImage->addClient(&client); | 212 cachedImage->addClient(&client); |
| 213 | 213 |
| 214 // Send the image response. | 214 // Send the image response. |
| 215 Vector<unsigned char> jpeg = jpegImage(); | 215 Vector<unsigned char> jpeg = jpegImage(); |
| 216 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); | 216 cachedImage->responseReceived(ResourceResponse(KURL(), "image/jpeg", jpeg.si
ze(), nullAtom, String()), nullptr); |
| 217 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); | 217 cachedImage->appendData(reinterpret_cast<const char*>(jpeg.data()), jpeg.siz
e()); |
| 218 cachedImage->finish(); | 218 cachedImage->finish(); |
| 219 ASSERT_FALSE(cachedImage->errorOccurred()); | 219 ASSERT_FALSE(cachedImage->errorOccurred()); |
| 220 ASSERT_TRUE(cachedImage->hasImage()); | 220 ASSERT_TRUE(cachedImage->hasImage()); |
| 221 ASSERT_FALSE(cachedImage->image()->isNull()); | 221 ASSERT_FALSE(cachedImage->image()->isNull()); |
| 222 ASSERT_EQ(client.imageChangedCount(), 2); | 222 ASSERT_EQ(client.imageChangedCount(), 2); |
| 223 ASSERT_TRUE(client.notifyFinishedCalled()); | 223 ASSERT_TRUE(client.notifyFinishedCalled()); |
| 224 | 224 |
| 225 HashSet<ImageResource*> bitmapImages; | 225 HashSet<ImageResource*> bitmapImages; |
| 226 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); | 226 ASSERT_TRUE(cachedImage->image()->isBitmapImage()); |
| 227 bitmapImages.add(cachedImage.get()); | 227 bitmapImages.add(cachedImage.get()); |
| 228 } | 228 } |
| 229 | 229 |
| 230 } // namespace blink | 230 } // namespace blink |
| OLD | NEW |