1 /*
2 * Copyright (C) 2011 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 * 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.
12 *
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.
23 */
24
25 #include "config.h"
26
27 #include <gtest/gtest.h>
28 #include "platform/graphics/GraphicsLayer.h"
29 #include "platform/graphics/Image.h"
30 #include "platform/graphics/skia/NativeImageSkia.h"
31 #include "public/platform/WebImageLayer.h"
32 #include "wtf/PassOwnPtr.h"
33
34 using namespace WebCore;
35
36 namespace {
37
38 class MockGraphicsLayerClient : public GraphicsLayerClient {
39 public:
notifyAnimationStarted(const GraphicsLayer *,double wallClockTime,double monotonicTime)40 virtual void notifyAnimationStarted(const GraphicsLayer*, double wallClockTime, double monotonicTime) OVERRIDE { }
paintContents(const GraphicsLayer *,GraphicsContext &,GraphicsLayerPaintingPhase,const IntRect & inClip)41 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE { }
debugName(const GraphicsLayer *)42 virtual String debugName(const GraphicsLayer*) OVERRIDE { return String(); }
43 };
44
45 class TestImage : public Image {
46 public:
47
create(const IntSize & size,SkAlphaType alphaType)48 static PassRefPtr<TestImage> create(const IntSize& size, SkAlphaType alphaType)
49 {
50 return adoptRef(new TestImage(size, alphaType));
51 }
52
TestImage(const IntSize & size,SkAlphaType alphaType)53 explicit TestImage(const IntSize& size, SkAlphaType alphaType)
54 : Image(0)
55 , m_size(size)
56 {
57 m_nativeImage = NativeImageSkia::create();
58 m_nativeImage->bitmap().setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height(), 0, alphaType);
59 m_nativeImage->bitmap().allocPixels();
60 }
61
isBitmapImage() const62 virtual bool isBitmapImage() const OVERRIDE
63 {
64 return true;
65 }
66
currentFrameKnownToBeOpaque()67 virtual bool currentFrameKnownToBeOpaque() OVERRIDE
68 {
69 return m_nativeImage->bitmap().isOpaque();
70 }
71
size() const72 virtual IntSize size() const OVERRIDE
73 {
74 return m_size;
75 }
76
nativeImageForCurrentFrame()77 virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE
78 {
79 if (m_size.isZero())
80 return 0;
81
82 return m_nativeImage;
83 }
84
85 // Stub implementations of pure virtual Image functions.
destroyDecodedData(bool)86 virtual void destroyDecodedData(bool) OVERRIDE
87 {
88 }
89
draw(GraphicsContext *,const FloatRect &,const FloatRect &,CompositeOperator,blink::WebBlendMode)90 virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, blink::WebBlendMode) OVERRIDE
91 {
92 }
93
94 private:
95
96 IntSize m_size;
97
98 RefPtr<NativeImageSkia> m_nativeImage;
99 };
100
101 class GraphicsLayerForTesting : public GraphicsLayer {
102 public:
GraphicsLayerForTesting(GraphicsLayerClient * client)103 explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
104 : GraphicsLayer(client) { };
105
contentsLayer() const106 virtual blink::WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
107 };
108
TEST(ImageLayerChromiumTest,opaqueImages)109 TEST(ImageLayerChromiumTest, opaqueImages)
110 {
111 MockGraphicsLayerClient client;
112 OwnPtr<GraphicsLayerForTesting> graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&client));
113 ASSERT_TRUE(graphicsLayer.get());
114
115 RefPtr<Image> opaqueImage = TestImage::create(IntSize(100, 100), kOpaque_SkAlphaType);
116 ASSERT_TRUE(opaqueImage.get());
117 RefPtr<Image> nonOpaqueImage = TestImage::create(IntSize(100, 100), kPremul_SkAlphaType);
118 ASSERT_TRUE(nonOpaqueImage.get());
119
120 ASSERT_FALSE(graphicsLayer->contentsLayer());
121
122 graphicsLayer->setContentsToImage(opaqueImage.get());
123 ASSERT_TRUE(graphicsLayer->contentsLayer()->opaque());
124
125 graphicsLayer->setContentsToImage(nonOpaqueImage.get());
126 ASSERT_FALSE(graphicsLayer->contentsLayer()->opaque());
127 }
128
129 } // namespace
130