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