4d547e9adcc000aa2914fd0dbf7cb4fc253cee96
[WebKit-https.git] / Source / WebKit / chromium / tests / GIFImageDecoderTest.cpp
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
33 #include "GIFImageDecoder.h"
34
35 #include "FileSystem.h"
36 #include "SharedBuffer.h"
37 #include <gtest/gtest.h>
38 #include <public/WebData.h>
39 #include <public/WebSize.h>
40 #include <webkit/support/webkit_support.h>
41 #include <wtf/OwnPtr.h>
42 #include <wtf/PassOwnPtr.h>
43
44 using namespace WebCore;
45 using namespace WebKit;
46
47 namespace {
48
49 static PassRefPtr<SharedBuffer> readFile(const char* fileName)
50 {
51     String filePath = webkit_support::GetWebKitRootDir();
52     filePath.append(fileName);
53
54     long long fileSize;
55     if (!getFileSize(filePath, fileSize))
56         return 0;
57
58     PlatformFileHandle handle = openFile(filePath, OpenForRead);
59     int fileLength = static_cast<int>(fileSize);
60     Vector<char> buffer(fileLength);
61     readFromFile(handle, buffer.data(), fileLength);
62     closeFile(handle);
63     return SharedBuffer::adoptVector(buffer);
64 }
65
66 TEST(GIFImageDecoderTest, decodeTwoFrames)
67 {
68     OwnPtr<GIFImageDecoder> decoder(adoptPtr(new GIFImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied)));
69
70     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/animated.gif");
71     ASSERT_TRUE(data.get());
72     decoder->setData(data.get(), true);
73     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
74
75     ImageFrame* frame = decoder->frameBufferAtIndex(0);
76     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
77     EXPECT_EQ(16, frame->getSkBitmap().width());
78     EXPECT_EQ(16, frame->getSkBitmap().height());
79
80     frame = decoder->frameBufferAtIndex(1);
81     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
82     EXPECT_EQ(16, frame->getSkBitmap().width());
83     EXPECT_EQ(16, frame->getSkBitmap().height());
84
85     EXPECT_EQ(2u, decoder->frameCount());
86     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
87 }
88
89 TEST(GIFImageDecoderTest, parseAndDecode)
90 {
91     OwnPtr<GIFImageDecoder> decoder(adoptPtr(new GIFImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied)));
92
93     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/animated.gif");
94     ASSERT_TRUE(data.get());
95     decoder->setData(data.get(), true);
96     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
97
98     // This call will parse the entire file.
99     EXPECT_EQ(2u, decoder->frameCount());
100
101     ImageFrame* frame = decoder->frameBufferAtIndex(0);
102     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
103     EXPECT_EQ(16, frame->getSkBitmap().width());
104     EXPECT_EQ(16, frame->getSkBitmap().height());
105
106     frame = decoder->frameBufferAtIndex(1);
107     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
108     EXPECT_EQ(16, frame->getSkBitmap().width());
109     EXPECT_EQ(16, frame->getSkBitmap().height());
110     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
111 }
112
113 TEST(GIFImageDecoderTest, parseByteByByte)
114 {
115     OwnPtr<GIFImageDecoder> decoder(adoptPtr(new GIFImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied)));
116
117     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/animated.gif");
118     ASSERT_TRUE(data.get());
119
120     size_t frameCount = 0;
121
122     // Pass data to decoder byte by byte.
123     for (unsigned length = 1; length <= data->size(); ++length) {
124         RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
125         decoder->setData(tempData.get(), length == data->size());
126
127         EXPECT_LE(frameCount, decoder->frameCount());
128         frameCount = decoder->frameCount();
129     }
130
131     EXPECT_EQ(2u, decoder->frameCount());
132
133     decoder->frameBufferAtIndex(0);
134     decoder->frameBufferAtIndex(1);
135     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
136 }
137
138 TEST(GIFImageDecoderTest, parseAndDecodeByteByByte)
139 {
140     OwnPtr<GIFImageDecoder> decoder(adoptPtr(new GIFImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied)));
141
142     RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/animated-gif-with-offsets.gif");
143     ASSERT_TRUE(data.get());
144
145     size_t frameCount = 0;
146     size_t framesDecoded = 0;
147
148     // Pass data to decoder byte by byte.
149     for (unsigned length = 1; length <= data->size(); ++length) {
150         RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length);
151         decoder->setData(tempData.get(), length == data->size());
152
153         EXPECT_LE(frameCount, decoder->frameCount());
154         frameCount = decoder->frameCount();
155
156         ImageFrame* frame = decoder->frameBufferAtIndex(frameCount - 1);
157         if (frame && frame->status() == ImageFrame::FrameComplete && framesDecoded < frameCount)
158             ++framesDecoded;
159     }
160
161     EXPECT_EQ(5u, decoder->frameCount());
162     EXPECT_EQ(5u, framesDecoded);
163     EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount());
164 }
165
166 // Second frame in the file is broken but test that first frame can be decoded.
167 TEST(GIFImageDecoderTest, brokenSecondFrame)
168 {
169     OwnPtr<GIFImageDecoder> decoder(adoptPtr(new GIFImageDecoder(ImageSource::AlphaNotPremultiplied, ImageSource::GammaAndColorProfileApplied)));
170
171     RefPtr<SharedBuffer> data = readFile("/Source/WebKit/chromium/tests/data/broken.gif");
172     ASSERT_TRUE(data.get());
173     decoder->setData(data.get(), true);
174
175     EXPECT_EQ(1u, decoder->frameCount());
176
177     ImageFrame* frame = decoder->frameBufferAtIndex(0);
178     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
179     EXPECT_EQ(16, frame->getSkBitmap().width());
180     EXPECT_EQ(16, frame->getSkBitmap().height());
181
182     frame = decoder->frameBufferAtIndex(1);
183     EXPECT_FALSE(frame);
184     EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount());
185 }
186
187 } // namespace