2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 #include "core/fetch/RawResource.h"
34 #include "core/fetch/ImageResourceClient.h"
35 #include "core/fetch/MemoryCache.h"
36 #include "core/fetch/MockImageResourceClient.h"
37 #include "core/fetch/ResourceFetcher.h"
38 #include "core/fetch/ResourcePtr.h"
39 #include "core/loader/DocumentLoader.h"
40 #include "core/testing/DummyPageHolder.h"
41 #include "core/testing/UnitTestHelpers.h"
42 #include "platform/SharedBuffer.h"
43 #include "public/platform/Platform.h"
44 #include "public/platform/WebURL.h"
45 #include "public/platform/WebURLResponse.h"
46 #include "public/platform/WebUnitTestSupport.h"
48 using namespace blink;
52 TEST(RawResourceTest, DontIgnoreAcceptForCacheReuse)
54 ResourceRequest jpegRequest;
55 jpegRequest.setHTTPAccept("image/jpeg");
57 ResourcePtr<RawResource> jpegResource(new RawResource(jpegRequest, Resource::Raw));
59 ResourceRequest pngRequest;
60 pngRequest.setHTTPAccept("image/png");
62 ASSERT_FALSE(jpegResource->canReuse(pngRequest));
65 TEST(RawResourceTest, RevalidationSucceeded)
67 // Create two RawResources and set one to revalidate the other.
68 RawResource* oldResourcePointer = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
69 RawResource* newResourcePointer = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
70 newResourcePointer->setResourceToRevalidate(oldResourcePointer);
71 ResourcePtr<Resource> oldResource = oldResourcePointer;
72 ResourcePtr<Resource> newResource = newResourcePointer;
73 memoryCache()->add(oldResource.get());
74 memoryCache()->remove(oldResource.get());
75 memoryCache()->add(newResource.get());
77 // Simulate a successful revalidation.
78 // The revalidated resource (oldResource) should now be in the cache, newResource
79 // should have been sliently switched to point to the revalidated resource, and
80 // we shouldn't hit any ASSERTs.
81 ResourceResponse response;
82 response.setHTTPStatusCode(304);
83 newResource->responseReceived(response, nullptr);
84 EXPECT_EQ(memoryCache()->resourceForURL(KURL(ParsedURLString, "data:text/html,")), oldResource.get());
85 EXPECT_EQ(oldResource.get(), newResource.get());
86 EXPECT_NE(newResource.get(), newResourcePointer);
89 class DummyClient : public RawResourceClient {
91 DummyClient() : m_called(false) { }
92 virtual ~DummyClient() { }
94 // ResourceClient implementation.
95 virtual void notifyFinished(Resource* resource)
100 bool called() { return m_called; }
105 // This client adds another client when notified.
106 class AddingClient : public RawResourceClient {
108 AddingClient(DummyClient* client, Resource* resource)
109 : m_dummyClient(client)
110 , m_resource(resource)
111 , m_removeClientTimer(this, &AddingClient::removeClient) { }
113 virtual ~AddingClient() { }
115 // ResourceClient implementation.
116 virtual void notifyFinished(Resource* resource)
118 // First schedule an asynchronous task to remove the client.
119 // We do not expect the client to be called.
120 m_removeClientTimer.startOneShot(0, FROM_HERE);
121 resource->addClient(m_dummyClient);
123 void removeClient(Timer<AddingClient>* timer)
125 m_resource->removeClient(m_dummyClient);
128 DummyClient* m_dummyClient;
129 Resource* m_resource;
130 Timer<AddingClient> m_removeClientTimer;
133 TEST(RawResourceTest, AddClientDuringCallback)
135 ResourcePtr<Resource> raw = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
136 raw->setLoading(false);
138 // Create a non-null response.
139 ResourceResponse response = raw->response();
140 response.setURL(KURL(ParsedURLString, "http://600.613/"));
141 raw->setResponse(response);
142 EXPECT_FALSE(raw->response().isNull());
144 OwnPtr<DummyClient> dummyClient = adoptPtr(new DummyClient());
145 OwnPtr<AddingClient> addingClient = adoptPtr(new AddingClient(dummyClient.get(), raw.get()));
146 raw->addClient(addingClient.get());
147 testing::runPendingTasks();
148 raw->removeClient(addingClient.get());
149 EXPECT_FALSE(dummyClient->called());
150 EXPECT_FALSE(raw->hasClients());
153 // This client removes another client when notified.
154 class RemovingClient : public RawResourceClient {
156 RemovingClient(DummyClient* client)
157 : m_dummyClient(client) { }
159 virtual ~RemovingClient() { }
161 // ResourceClient implementation.
162 virtual void notifyFinished(Resource* resource)
164 resource->removeClient(m_dummyClient);
165 resource->removeClient(this);
168 DummyClient* m_dummyClient;
171 TEST(RawResourceTest, RemoveClientDuringCallback)
173 ResourcePtr<Resource> raw = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
174 raw->setLoading(false);
176 // Create a non-null response.
177 ResourceResponse response = raw->response();
178 response.setURL(KURL(ParsedURLString, "http://600.613/"));
179 raw->setResponse(response);
180 EXPECT_FALSE(raw->response().isNull());
182 OwnPtr<DummyClient> dummyClient = adoptPtr(new DummyClient());
183 OwnPtr<RemovingClient> removingClient = adoptPtr(new RemovingClient(dummyClient.get()));
184 raw->addClient(dummyClient.get());
185 raw->addClient(removingClient.get());
186 testing::runPendingTasks();
187 EXPECT_FALSE(raw->hasClients());