2 * Copyright (C) 2012 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef MockImageDecoder_h
28 #include "platform/image-decoders/ImageDecoder.h"
29 #include "wtf/PassOwnPtr.h"
33 class MockImageDecoderClient {
35 virtual void decoderBeingDestroyed() = 0;
36 virtual void frameBufferRequested() = 0;
37 virtual ImageFrame::Status status() = 0;
38 virtual size_t frameCount() = 0;
39 virtual int repetitionCount() const = 0;
40 virtual float frameDuration() const = 0;
42 // Clients can control the behavior of MockImageDecoder::decodedSize() by
43 // overriding this method. The default implementation causes
44 // MockImageDecoder::decodedSize() to return the same thing as
45 // MockImageDecoder::size(). See the precise implementation of
46 // MockImageDecoder::decodedSize() below.
47 virtual IntSize decodedSize() const { return IntSize(); }
50 class MockImageDecoder : public ImageDecoder {
52 static PassOwnPtr<MockImageDecoder> create(MockImageDecoderClient* client) { return adoptPtr(new MockImageDecoder(client)); }
54 MockImageDecoder(MockImageDecoderClient* client)
55 : ImageDecoder(ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileApplied, noDecodedImageByteLimit)
61 m_client->decoderBeingDestroyed();
64 virtual IntSize decodedSize() const override
66 return m_client->decodedSize().isEmpty() ? size() : m_client->decodedSize();
69 virtual bool setSize(unsigned width, unsigned height) override
71 ImageDecoder::setSize(width, height);
72 m_frameBufferCache.resize(1);
73 m_frameBufferCache[0].setSize(width, height);
77 virtual String filenameExtension() const override
82 virtual size_t frameCount() override
84 return m_client->frameCount();
87 virtual int repetitionCount() const override
89 return m_client->repetitionCount();
92 virtual ImageFrame* frameBufferAtIndex(size_t) override
94 m_client->frameBufferRequested();
96 m_frameBufferCache[0].setStatus(m_client->status());
97 return &m_frameBufferCache[0];
100 virtual bool frameIsCompleteAtIndex(size_t) const override
102 return m_client->status() == ImageFrame::FrameComplete;
105 virtual float frameDurationAtIndex(size_t) const override
107 return m_client->frameDuration();
110 void setFrameHasAlpha(bool hasAlpha) { m_frameBufferCache[0].setHasAlpha(hasAlpha); }
113 MockImageDecoderClient* m_client;
116 class MockImageDecoderFactory : public ImageDecoderFactory {
118 static PassOwnPtr<MockImageDecoderFactory> create(MockImageDecoderClient* client, const SkISize& decodedSize)
120 return adoptPtr(new MockImageDecoderFactory(client, IntSize(decodedSize.width(), decodedSize.height())));
123 static PassOwnPtr<MockImageDecoderFactory> create(MockImageDecoderClient* client, const IntSize& decodedSize)
125 return adoptPtr(new MockImageDecoderFactory(client, decodedSize));
128 virtual PassOwnPtr<ImageDecoder> create() override
130 OwnPtr<MockImageDecoder> decoder = MockImageDecoder::create(m_client);
131 decoder->setSize(m_decodedSize.width(), m_decodedSize.height());
132 decoder->setFrameHasAlpha(false);
133 return decoder.release();
137 MockImageDecoderFactory(MockImageDecoderClient* client, const IntSize& decodedSize)
139 , m_decodedSize(decodedSize)
143 MockImageDecoderClient* m_client;
144 IntSize m_decodedSize;
149 #endif // MockImageDecoder_h