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