1/*
2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
29 */
30
31#include "config.h"
32#include "core/loader/cache/ImageResource.h"
33
34#include "core/loader/DocumentLoader.h"
35#include "core/loader/EmptyClients.h"
36#include "core/loader/cache/ImageResourceClient.h"
37#include "core/loader/cache/MemoryCache.h"
38#include "core/loader/cache/MockImageResourceClient.h"
39#include "core/loader/cache/ResourceFetcher.h"
40#include "core/loader/cache/ResourcePtr.h"
41#include "core/page/Frame.h"
42#include "core/page/FrameView.h"
43#include "core/page/Page.h"
44#include "core/platform/SharedBuffer.h"
45#include "public/platform/Platform.h"
46#include "public/platform/WebThread.h"
47#include "public/platform/WebURL.h"
48#include "public/platform/WebURLResponse.h"
49#include "public/platform/WebUnitTestSupport.h"
50
51using namespace WebCore;
52
53namespace {
54
55class QuitTask : public WebKit::WebThread::Task {
56public:
57    virtual void run()
58    {
59        WebKit::Platform::current()->currentThread()->exitRunLoop();
60    }
61};
62
63void runPendingTasks()
64{
65    WebKit::Platform::current()->currentThread()->postTask(new QuitTask);
66    WebKit::Platform::current()->currentThread()->enterRunLoop();
67}
68
69TEST(ImageResourceTest, MultipartImage)
70{
71    ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest());
72    cachedImage->setLoading(true);
73
74    MockImageResourceClient client;
75    cachedImage->addClient(&client);
76
77    // Send the multipart response. No image or data buffer is created.
78    cachedImage->responseReceived(ResourceResponse(KURL(), "multipart/x-mixed-replace", 0, String(), String()));
79    ASSERT_FALSE(cachedImage->resourceBuffer());
80    ASSERT_FALSE(cachedImage->hasImage());
81    ASSERT_EQ(client.imageChangedCount(), 0);
82    ASSERT_FALSE(client.notifyFinishedCalled());
83
84    // Send the response for the first real part. No image or data buffer is created.
85    const char* svgData = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1' fill='green'/></svg>";
86    unsigned svgDataLength = strlen(svgData);
87    cachedImage->responseReceived(ResourceResponse(KURL(), "image/svg+xml", svgDataLength, String(), String()));
88    ASSERT_FALSE(cachedImage->resourceBuffer());
89    ASSERT_FALSE(cachedImage->hasImage());
90    ASSERT_EQ(client.imageChangedCount(), 0);
91    ASSERT_FALSE(client.notifyFinishedCalled());
92
93    // The first bytes arrive. The data buffer is created, but no image is created.
94    cachedImage->appendData(svgData, svgDataLength);
95    ASSERT_TRUE(cachedImage->resourceBuffer());
96    ASSERT_EQ(cachedImage->resourceBuffer()->size(), svgDataLength);
97    ASSERT_FALSE(cachedImage->hasImage());
98    ASSERT_EQ(client.imageChangedCount(), 0);
99    ASSERT_FALSE(client.notifyFinishedCalled());
100
101    // This part finishes. The image is created, callbacks are sent, and the data buffer is cleared.
102    cachedImage->finish();
103    ASSERT_FALSE(cachedImage->resourceBuffer());
104    ASSERT_FALSE(cachedImage->errorOccurred());
105    ASSERT_TRUE(cachedImage->hasImage());
106    ASSERT_FALSE(cachedImage->image()->isNull());
107    ASSERT_EQ(cachedImage->image()->width(), 1);
108    ASSERT_EQ(cachedImage->image()->height(), 1);
109    ASSERT_EQ(client.imageChangedCount(), 2);
110    ASSERT_TRUE(client.notifyFinishedCalled());
111}
112
113TEST(ImageResourceTest, CancelOnDetach)
114{
115    KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
116
117    WebKit::WebURLResponse response;
118    response.initialize();
119    response.setMIMEType("text/html");
120    WTF::String localPath = WebKit::Platform::current()->unitTestSupport()->webKitRootDir();
121    localPath.append("/Source/web/tests/data/cancelTest.html");
122    WebKit::Platform::current()->unitTestSupport()->registerMockedURL(testURL, response, localPath);
123
124    // Create enough of a mocked world to get a functioning ResourceLoader.
125    Page::PageClients pageClients;
126    fillWithEmptyClients(pageClients);
127    EmptyFrameLoaderClient frameLoaderClient;
128    Page page(pageClients);
129    RefPtr<Frame> frame = Frame::create(&page, 0, &frameLoaderClient);
130    frame->setView(FrameView::create(frame.get()));
131    frame->init();
132    RefPtr<DocumentLoader> documentLoader = DocumentLoader::create(ResourceRequest(testURL), SubstituteData());
133    documentLoader->setFrame(frame.get());
134
135    // Emulate starting a real load.
136    ResourcePtr<ImageResource> cachedImage = new ImageResource(ResourceRequest(testURL));
137    cachedImage->load(documentLoader->fetcher(), ResourceLoaderOptions());
138    memoryCache()->add(cachedImage.get());
139
140    MockImageResourceClient client;
141    cachedImage->addClient(&client);
142    EXPECT_EQ(Resource::Pending, cachedImage->status());
143
144    // The load should still be alive, but a timer should be started to cancel the load inside removeClient().
145    cachedImage->removeClient(&client);
146    EXPECT_EQ(Resource::Pending, cachedImage->status());
147    EXPECT_NE(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
148
149    // Trigger the cancel timer, ensure the load was cancelled and the resource was evicted from the cache.
150    runPendingTasks();
151    EXPECT_EQ(Resource::LoadError, cachedImage->status());
152    EXPECT_EQ(reinterpret_cast<Resource*>(0), memoryCache()->resourceForURL(testURL));
153
154    WebKit::Platform::current()->unitTestSupport()->unregisterMockedURL(testURL);
155}
156
157} // namespace
158