1 /* 2 * Copyright (C) 2012 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 6 * are met: 7 * 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. 13 * 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. 24 */ 25 26 #ifndef MockImageDecoder_h 27 28 #include "platform/image-decoders/ImageDecoder.h" 29 #include "wtf/PassOwnPtr.h" 30 31 namespace WebCore { 32 33 class MockImageDecoderClient { 34 public: 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; 41 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(); } 48 }; 49 50 class MockImageDecoder : public ImageDecoder { 51 public: 52 static PassOwnPtr<MockImageDecoder> create(MockImageDecoderClient* client) { return adoptPtr(new MockImageDecoder(client)); } 53 54 MockImageDecoder(MockImageDecoderClient* client) 55 : ImageDecoder(ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileApplied, noDecodedImageByteLimit) 56 , m_client(client) 57 { } 58 59 ~MockImageDecoder() 60 { 61 m_client->decoderBeingDestroyed(); 62 } 63 64 virtual IntSize decodedSize() const OVERRIDE 65 { 66 return m_client->decodedSize().isEmpty() ? size() : m_client->decodedSize(); 67 } 68 69 virtual bool setSize(unsigned width, unsigned height) OVERRIDE 70 { 71 ImageDecoder::setSize(width, height); 72 m_frameBufferCache.resize(1); 73 m_frameBufferCache[0].setSize(width, height); 74 return true; 75 } 76 77 virtual String filenameExtension() const OVERRIDE 78 { 79 return "mock"; 80 } 81 82 virtual size_t frameCount() OVERRIDE 83 { 84 return m_client->frameCount(); 85 } 86 87 virtual int repetitionCount() const OVERRIDE 88 { 89 return m_client->repetitionCount(); 90 } 91 92 virtual ImageFrame* frameBufferAtIndex(size_t) OVERRIDE 93 { 94 m_client->frameBufferRequested(); 95 96 m_frameBufferCache[0].setStatus(m_client->status()); 97 return &m_frameBufferCache[0]; 98 } 99 100 virtual bool frameIsCompleteAtIndex(size_t) const OVERRIDE 101 { 102 return m_client->status() == ImageFrame::FrameComplete; 103 } 104 105 virtual float frameDurationAtIndex(size_t) const OVERRIDE 106 { 107 return m_client->frameDuration(); 108 } 109 110 void setFrameHasAlpha(bool hasAlpha) { m_frameBufferCache[0].setHasAlpha(hasAlpha); } 111 112 private: 113 MockImageDecoderClient* m_client; 114 }; 115 116 class MockImageDecoderFactory : public ImageDecoderFactory { 117 public: 118 static PassOwnPtr<MockImageDecoderFactory> create(MockImageDecoderClient* client, const SkISize& decodedSize) 119 { 120 return adoptPtr(new MockImageDecoderFactory(client, IntSize(decodedSize.width(), decodedSize.height()))); 121 } 122 123 static PassOwnPtr<MockImageDecoderFactory> create(MockImageDecoderClient* client, const IntSize& decodedSize) 124 { 125 return adoptPtr(new MockImageDecoderFactory(client, decodedSize)); 126 } 127 128 virtual PassOwnPtr<ImageDecoder> create() OVERRIDE 129 { 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(); 134 } 135 136 private: 137 MockImageDecoderFactory(MockImageDecoderClient* client, const IntSize& decodedSize) 138 : m_client(client) 139 , m_decodedSize(decodedSize) 140 { 141 } 142 143 MockImageDecoderClient* m_client; 144 IntSize m_decodedSize; 145 }; 146 147 } // namespace WebCore 148 149 #endif // MockImageDecoder_h 150