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 "third_party/skia/include/core/SkImage.h"
30 #include "third_party/skia/include/core/SkSurface.h"
31 #include "wtf/PassOwnPtr.h"
32 #include <gtest/gtest.h>
38 class MockGraphicsLayerClient
: public GraphicsLayerClient
{
40 void paintContents(const GraphicsLayer
*, GraphicsContext
&, GraphicsLayerPaintingPhase
, const IntRect
&) override
{ }
41 String
debugName(const GraphicsLayer
*) override
{ return String(); }
44 class TestImage
: public Image
{
46 static PassRefPtr
<TestImage
> create(const IntSize
& size
, bool isOpaque
)
48 return adoptRef(new TestImage(size
, isOpaque
));
51 TestImage(const IntSize
& size
, bool isOpaque
)
55 RefPtr
<SkSurface
> surface
= adoptRef(SkSurface::NewRaster(SkImageInfo::MakeN32(
56 size
.width(), size
.height(), isOpaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
)));
60 surface
->getCanvas()->clear(SK_ColorTRANSPARENT
);
61 m_image
= adoptRef(surface
->newImageSnapshot());
64 bool isBitmapImage() const override
69 bool currentFrameKnownToBeOpaque() override
71 return m_image
->isOpaque();
74 IntSize
size() const override
79 PassRefPtr
<SkImage
> imageForCurrentFrame() override
84 // Stub implementations of pure virtual Image functions.
85 void destroyDecodedData(bool) override
89 void draw(SkCanvas
*, const SkPaint
&, const FloatRect
&, const FloatRect
&, RespectImageOrientationEnum
, ImageClampingMode
) override
95 RefPtr
<SkImage
> m_image
;
98 class GraphicsLayerForTesting
: public GraphicsLayer
{
100 explicit GraphicsLayerForTesting(GraphicsLayerClient
* client
)
101 : GraphicsLayer(client
) { }
103 WebLayer
* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
106 } // anonymous namespace
108 TEST(ImageLayerChromiumTest
, imageLayerContentReset
)
110 MockGraphicsLayerClient client
;
111 OwnPtr
<GraphicsLayerForTesting
> graphicsLayer
= adoptPtr(new GraphicsLayerForTesting(&client
));
112 ASSERT_TRUE(graphicsLayer
.get());
114 ASSERT_FALSE(graphicsLayer
->hasContentsLayer());
115 ASSERT_FALSE(graphicsLayer
->contentsLayer());
117 RefPtr
<Image
> image
= TestImage::create(IntSize(100, 100), false);
118 ASSERT_TRUE(image
.get());
120 graphicsLayer
->setContentsToImage(image
.get());
121 ASSERT_TRUE(graphicsLayer
->hasContentsLayer());
122 ASSERT_TRUE(graphicsLayer
->contentsLayer());
124 graphicsLayer
->setContentsToImage(0);
125 ASSERT_FALSE(graphicsLayer
->hasContentsLayer());
126 ASSERT_FALSE(graphicsLayer
->contentsLayer());
129 TEST(ImageLayerChromiumTest
, opaqueImages
)
131 MockGraphicsLayerClient client
;
132 OwnPtr
<GraphicsLayerForTesting
> graphicsLayer
= adoptPtr(new GraphicsLayerForTesting(&client
));
133 ASSERT_TRUE(graphicsLayer
.get());
135 RefPtr
<Image
> opaqueImage
= TestImage::create(IntSize(100, 100), true /* opaque */);
136 ASSERT_TRUE(opaqueImage
.get());
137 RefPtr
<Image
> nonOpaqueImage
= TestImage::create(IntSize(100, 100), false /* opaque */);
138 ASSERT_TRUE(nonOpaqueImage
.get());
140 ASSERT_FALSE(graphicsLayer
->contentsLayer());
142 graphicsLayer
->setContentsToImage(opaqueImage
.get());
143 ASSERT_TRUE(graphicsLayer
->contentsLayer()->opaque());
145 graphicsLayer
->setContentsToImage(nonOpaqueImage
.get());
146 ASSERT_FALSE(graphicsLayer
->contentsLayer()->opaque());