2 * Copyright (C) 2011 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "platform/graphics/Image.h"
28 #include "platform/graphics/GraphicsLayer.h"
29 #include "platform/graphics/skia/NativeImageSkia.h"
30 #include "wtf/PassOwnPtr.h"
32 #include <gtest/gtest.h>
34 using namespace blink;
38 class MockGraphicsLayerClient : public GraphicsLayerClient {
40 virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE { }
41 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE { }
42 virtual String debugName(const GraphicsLayer*) OVERRIDE { return String(); }
45 class TestImage : public Image {
48 static PassRefPtr<TestImage> create(const IntSize& size, bool isOpaque)
50 return adoptRef(new TestImage(size, isOpaque));
53 explicit TestImage(const IntSize& size, bool isOpaque)
58 bitmap.allocN32Pixels(size.width(), size.height(), isOpaque);
59 m_nativeImage = NativeImageSkia::create(bitmap);
62 virtual bool isBitmapImage() const OVERRIDE
67 virtual bool currentFrameKnownToBeOpaque() OVERRIDE
69 return m_nativeImage->bitmap().isOpaque();
72 virtual IntSize size() const OVERRIDE
77 virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE
85 // Stub implementations of pure virtual Image functions.
86 virtual void destroyDecodedData(bool) OVERRIDE
90 virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, WebBlendMode) OVERRIDE
98 RefPtr<NativeImageSkia> m_nativeImage;
101 class GraphicsLayerForTesting : public GraphicsLayer {
103 explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
104 : GraphicsLayer(client) { };
106 virtual WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
109 TEST(ImageLayerChromiumTest, imageLayerContentReset)
111 MockGraphicsLayerClient client;
112 OwnPtr<GraphicsLayerForTesting> graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&client));
113 ASSERT_TRUE(graphicsLayer.get());
115 ASSERT_FALSE(graphicsLayer->hasContentsLayer());
116 ASSERT_FALSE(graphicsLayer->contentsLayer());
118 RefPtr<Image> image = TestImage::create(IntSize(100, 100), false);
119 ASSERT_TRUE(image.get());
121 graphicsLayer->setContentsToImage(image.get());
122 ASSERT_TRUE(graphicsLayer->hasContentsLayer());
123 ASSERT_TRUE(graphicsLayer->contentsLayer());
125 graphicsLayer->setContentsToImage(0);
126 ASSERT_FALSE(graphicsLayer->hasContentsLayer());
127 ASSERT_FALSE(graphicsLayer->contentsLayer());
130 TEST(ImageLayerChromiumTest, opaqueImages)
132 MockGraphicsLayerClient client;
133 OwnPtr<GraphicsLayerForTesting> graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&client));
134 ASSERT_TRUE(graphicsLayer.get());
136 RefPtr<Image> opaqueImage = TestImage::create(IntSize(100, 100), true /* opaque */);
137 ASSERT_TRUE(opaqueImage.get());
138 RefPtr<Image> nonOpaqueImage = TestImage::create(IntSize(100, 100), false /* opaque */);
139 ASSERT_TRUE(nonOpaqueImage.get());
141 ASSERT_FALSE(graphicsLayer->contentsLayer());
143 graphicsLayer->setContentsToImage(opaqueImage.get());
144 ASSERT_TRUE(graphicsLayer->contentsLayer()->opaque());
146 graphicsLayer->setContentsToImage(nonOpaqueImage.get());
147 ASSERT_FALSE(graphicsLayer->contentsLayer()->opaque());