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

Side by Side Diff: Source/core/loader/cache/CachedImageTest.cpp

Issue 17436003: Evict CachedResources if they are cancelled due to last client being removed. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 6 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/core/loader/cache/CachedResource.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "core/loader/cache/CachedImage.h" 32 #include "core/loader/cache/CachedImage.h"
33 33
34 #include "core/loader/DocumentLoader.h"
35 #include "core/loader/EmptyClients.h"
34 #include "core/loader/cache/CachedImageClient.h" 36 #include "core/loader/cache/CachedImageClient.h"
35 #include "core/loader/cache/CachedResourceHandle.h" 37 #include "core/loader/cache/CachedResourceHandle.h"
38 #include "core/loader/cache/CachedResourceLoader.h"
39 #include "core/loader/cache/MemoryCache.h"
40 #include "core/page/Frame.h"
41 #include "core/page/FrameView.h"
42 #include "core/page/Page.h"
36 #include "core/platform/SharedBuffer.h" 43 #include "core/platform/SharedBuffer.h"
37 #include "core/platform/graphics/Image.h" 44 #include "core/platform/graphics/Image.h"
45 #include "public/platform/Platform.h"
46 #include "public/platform/WebThread.h"
38 #include <gtest/gtest.h> 47 #include <gtest/gtest.h>
39 48
40 using namespace WebCore; 49 using namespace WebCore;
41 50
42 namespace { 51 namespace {
43 52
44 class MockCachedImageClient : public WebCore::CachedImageClient { 53 class MockCachedImageClient : public WebCore::CachedImageClient {
45 public: 54 public:
46 MockCachedImageClient() 55 MockCachedImageClient()
47 : m_imageChangedCount(0) 56 : m_imageChangedCount(0)
(...skipping 14 matching lines...) Expand all
62 } 71 }
63 72
64 int imageChangedCount() const { return m_imageChangedCount; } 73 int imageChangedCount() const { return m_imageChangedCount; }
65 bool notifyFinishedCalled() const { return m_notifyFinishedCalled; } 74 bool notifyFinishedCalled() const { return m_notifyFinishedCalled; }
66 75
67 private: 76 private:
68 int m_imageChangedCount; 77 int m_imageChangedCount;
69 bool m_notifyFinishedCalled; 78 bool m_notifyFinishedCalled;
70 }; 79 };
71 80
81 class QuitTask : public WebKit::WebThread::Task {
82 public:
83 virtual void run()
84 {
85 WebKit::Platform::current()->currentThread()->exitRunLoop();
86 }
87 };
88
89 void runPendingTasks()
90 {
91 WebKit::Platform::current()->currentThread()->postTask(new QuitTask);
92 WebKit::Platform::current()->currentThread()->enterRunLoop();
93 }
94
72 TEST(CachedImageTest, MultipartImage) 95 TEST(CachedImageTest, MultipartImage)
73 { 96 {
74 CachedResourceHandle<CachedImage> cachedImage = new CachedImage(ResourceRequ est()); 97 CachedResourceHandle<CachedImage> cachedImage = new CachedImage(ResourceRequ est());
75 cachedImage->setLoading(true); 98 cachedImage->setLoading(true);
76 99
77 MockCachedImageClient client; 100 MockCachedImageClient client;
78 cachedImage->addClient(&client); 101 cachedImage->addClient(&client);
79 102
80 // Send the multipart response. No image or data buffer is created. 103 // Send the multipart response. No image or data buffer is created.
81 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, String(), String())); 104 cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-re place", 0, String(), String()));
(...skipping 24 matching lines...) Expand all
106 ASSERT_FALSE(cachedImage->resourceBuffer()); 129 ASSERT_FALSE(cachedImage->resourceBuffer());
107 ASSERT_FALSE(cachedImage->errorOccurred()); 130 ASSERT_FALSE(cachedImage->errorOccurred());
108 ASSERT_TRUE(cachedImage->hasImage()); 131 ASSERT_TRUE(cachedImage->hasImage());
109 ASSERT_FALSE(cachedImage->image()->isNull()); 132 ASSERT_FALSE(cachedImage->image()->isNull());
110 ASSERT_EQ(cachedImage->image()->width(), 1); 133 ASSERT_EQ(cachedImage->image()->width(), 1);
111 ASSERT_EQ(cachedImage->image()->height(), 1); 134 ASSERT_EQ(cachedImage->image()->height(), 1);
112 ASSERT_EQ(client.imageChangedCount(), 2); 135 ASSERT_EQ(client.imageChangedCount(), 2);
113 ASSERT_TRUE(client.notifyFinishedCalled()); 136 ASSERT_TRUE(client.notifyFinishedCalled());
114 } 137 }
115 138
139 TEST(CachedImageTest, CancelOnDetach)
140 {
141 // Create enough of a mocked world to get a functioning ResourceLoader.
142 KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
143 Page::PageClients pageClients;
144 fillWithEmptyClients(pageClients);
145 EmptyFrameLoaderClient frameLoaderClient;
146 Page page(pageClients);
147 RefPtr<Frame> frame = Frame::create(&page, 0, &frameLoaderClient);
148 frame->setView(FrameView::create(frame.get()));
149 frame->init();
150 RefPtr<DocumentLoader> documentLoader = DocumentLoader::create(ResourceReque st(testURL), SubstituteData());
151 documentLoader->setFrame(frame.get());
152
153 // Emulate starting a real load.
154 CachedResourceHandle<CachedImage> cachedImage = new CachedImage(ResourceRequ est(testURL));
155 cachedImage->load(documentLoader->cachedResourceLoader(), ResourceLoaderOpti ons());
156 memoryCache()->add(cachedImage.get());
157
158 MockCachedImageClient client;
159 cachedImage->addClient(&client);
160 EXPECT_EQ(CachedResource::Pending, cachedImage->status());
161
162 // The load should still be alive, but a timer should be started to cancel t he load inside removeClient().
163 cachedImage->removeClient(&client);
164 EXPECT_EQ(CachedResource::Pending, cachedImage->status());
165 EXPECT_NE(reinterpret_cast<CachedResource*>(0), memoryCache()->resourceForUR L(testURL));
166
167 // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
168 runPendingTasks();
169 EXPECT_EQ(CachedResource::LoadError, cachedImage->status());
170 EXPECT_EQ(reinterpret_cast<CachedResource*>(0), memoryCache()->resourceForUR L(testURL));
171 }
172
116 } // namespace 173 } // namespace
OLDNEW
« no previous file with comments | « no previous file | Source/core/loader/cache/CachedResource.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698