Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / RawResourceTest.cpp
blob2b0a4801469f83139dadab5eee8d11da6dc99169
1 /*
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
6 * met:
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
13 * distribution.
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.
31 #include "config.h"
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 "platform/SharedBuffer.h"
40 #include "platform/testing/UnitTestHelpers.h"
41 #include "public/platform/Platform.h"
42 #include "public/platform/WebURL.h"
43 #include "public/platform/WebURLResponse.h"
44 #include "public/platform/WebUnitTestSupport.h"
46 #include <gtest/gtest.h>
48 namespace blink {
50 TEST(RawResourceTest, DontIgnoreAcceptForCacheReuse)
52 ResourceRequest jpegRequest;
53 jpegRequest.setHTTPAccept("image/jpeg");
55 ResourcePtr<RawResource> jpegResource(new RawResource(jpegRequest, Resource::Raw));
57 ResourceRequest pngRequest;
58 pngRequest.setHTTPAccept("image/png");
60 ASSERT_FALSE(jpegResource->canReuse(pngRequest));
63 TEST(RawResourceTest, RevalidationSucceeded)
65 // Create two RawResources and set one to revalidate the other.
66 RawResource* oldResourcePointer = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
67 RawResource* newResourcePointer = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
68 newResourcePointer->setResourceToRevalidate(oldResourcePointer);
69 ResourcePtr<Resource> oldResource = oldResourcePointer;
70 ResourcePtr<Resource> newResource = newResourcePointer;
71 memoryCache()->add(oldResource.get());
72 memoryCache()->remove(oldResource.get());
73 memoryCache()->add(newResource.get());
75 // Simulate a successful revalidation.
76 // The revalidated resource (oldResource) should now be in the cache, newResource
77 // should have been sliently switched to point to the revalidated resource, and
78 // we shouldn't hit any ASSERTs.
79 ResourceResponse response;
80 response.setHTTPStatusCode(304);
81 newResource->responseReceived(response, nullptr);
82 EXPECT_EQ(memoryCache()->resourceForURL(KURL(ParsedURLString, "data:text/html,")), oldResource.get());
83 EXPECT_EQ(oldResource.get(), newResource.get());
84 EXPECT_NE(newResource.get(), newResourcePointer);
85 memoryCache()->remove(newResource.get());
88 class DummyClient : public RawResourceClient {
89 public:
90 DummyClient() : m_called(false) {}
91 ~DummyClient() override {}
93 // ResourceClient implementation.
94 virtual void notifyFinished(Resource* resource)
96 m_called = true;
99 bool called() { return m_called; }
100 private:
101 bool m_called;
104 // This client adds another client when notified.
105 class AddingClient : public RawResourceClient {
106 public:
107 AddingClient(DummyClient* client, Resource* resource)
108 : m_dummyClient(client)
109 , m_resource(resource)
110 , m_removeClientTimer(this, &AddingClient::removeClient) {}
112 ~AddingClient() override {}
114 // ResourceClient implementation.
115 virtual void notifyFinished(Resource* resource)
117 // First schedule an asynchronous task to remove the client.
118 // We do not expect the client to be called.
119 m_removeClientTimer.startOneShot(0, FROM_HERE);
120 resource->addClient(m_dummyClient);
122 void removeClient(Timer<AddingClient>* timer)
124 m_resource->removeClient(m_dummyClient);
126 private:
127 DummyClient* m_dummyClient;
128 ResourcePtr<Resource> m_resource;
129 Timer<AddingClient> m_removeClientTimer;
132 TEST(RawResourceTest, AddClientDuringCallback)
134 ResourcePtr<Resource> raw = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
135 raw->setLoading(false);
137 // Create a non-null response.
138 ResourceResponse response = raw->response();
139 response.setURL(KURL(ParsedURLString, "http://600.613/"));
140 raw->setResponse(response);
141 EXPECT_FALSE(raw->response().isNull());
143 OwnPtr<DummyClient> dummyClient = adoptPtr(new DummyClient());
144 OwnPtr<AddingClient> addingClient = adoptPtr(new AddingClient(dummyClient.get(), raw.get()));
145 raw->addClient(addingClient.get());
146 testing::runPendingTasks();
147 raw->removeClient(addingClient.get());
148 EXPECT_FALSE(dummyClient->called());
149 EXPECT_FALSE(raw->hasClients());
152 // This client removes another client when notified.
153 class RemovingClient : public RawResourceClient {
154 public:
155 RemovingClient(DummyClient* client)
156 : m_dummyClient(client) {}
158 ~RemovingClient() override {}
160 // ResourceClient implementation.
161 virtual void notifyFinished(Resource* resource)
163 resource->removeClient(m_dummyClient);
164 resource->removeClient(this);
166 private:
167 DummyClient* m_dummyClient;
170 TEST(RawResourceTest, RemoveClientDuringCallback)
172 ResourcePtr<Resource> raw = new RawResource(ResourceRequest("data:text/html,"), Resource::Raw);
173 raw->setLoading(false);
175 // Create a non-null response.
176 ResourceResponse response = raw->response();
177 response.setURL(KURL(ParsedURLString, "http://600.613/"));
178 raw->setResponse(response);
179 EXPECT_FALSE(raw->response().isNull());
181 OwnPtr<DummyClient> dummyClient = adoptPtr(new DummyClient());
182 OwnPtr<RemovingClient> removingClient = adoptPtr(new RemovingClient(dummyClient.get()));
183 raw->addClient(dummyClient.get());
184 raw->addClient(removingClient.get());
185 testing::runPendingTasks();
186 EXPECT_FALSE(raw->hasClients());
189 } // namespace blink