Home | History | Annotate | Download | only in tests
      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  * 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 "platform/graphics/GraphicsLayer.h"
     28 
     29 #include "platform/scroll/ScrollableArea.h"
     30 #include "platform/transforms/Matrix3DTransformOperation.h"
     31 #include "platform/transforms/RotateTransformOperation.h"
     32 #include "platform/transforms/TranslateTransformOperation.h"
     33 #include "wtf/PassOwnPtr.h"
     34 
     35 #include <gtest/gtest.h>
     36 #include "public/platform/Platform.h"
     37 #include "public/platform/WebCompositorSupport.h"
     38 #include "public/platform/WebFloatAnimationCurve.h"
     39 #include "public/platform/WebGraphicsContext3D.h"
     40 #include "public/platform/WebLayer.h"
     41 #include "public/platform/WebLayerTreeView.h"
     42 #include "public/platform/WebUnitTestSupport.h"
     43 
     44 using namespace WebCore;
     45 using namespace blink;
     46 
     47 namespace {
     48 
     49 class MockGraphicsLayerClient : public GraphicsLayerClient {
     50 public:
     51     virtual void notifyAnimationStarted(const GraphicsLayer*, double wallClockTime, double monotonicTime) OVERRIDE { }
     52     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE { }
     53     virtual String debugName(const GraphicsLayer*) OVERRIDE { return String(); }
     54 };
     55 
     56 class GraphicsLayerForTesting : public GraphicsLayer {
     57 public:
     58     explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
     59         : GraphicsLayer(client) { };
     60 
     61     virtual blink::WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
     62 };
     63 
     64 class GraphicsLayerTest : public testing::Test {
     65 public:
     66     GraphicsLayerTest()
     67     {
     68         m_graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&m_client));
     69         m_platformLayer = m_graphicsLayer->platformLayer();
     70         m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
     71         ASSERT(m_layerTreeView);
     72         m_layerTreeView->setRootLayer(*m_platformLayer);
     73         m_layerTreeView->setViewportSize(WebSize(1, 1), WebSize(1, 1));
     74     }
     75 
     76     virtual ~GraphicsLayerTest()
     77     {
     78         m_graphicsLayer.clear();
     79         m_layerTreeView.clear();
     80     }
     81 
     82 protected:
     83     WebLayer* m_platformLayer;
     84     OwnPtr<GraphicsLayerForTesting> m_graphicsLayer;
     85 
     86 private:
     87     OwnPtr<WebLayerTreeView> m_layerTreeView;
     88     MockGraphicsLayerClient m_client;
     89 };
     90 
     91 TEST_F(GraphicsLayerTest, updateLayerPreserves3DWithAnimations)
     92 {
     93     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
     94 
     95     OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(Platform::current()->compositorSupport()->createFloatAnimationCurve());
     96     curve->add(WebFloatKeyframe(0.0, 0.0));
     97     OwnPtr<WebAnimation> floatAnimation(adoptPtr(Platform::current()->compositorSupport()->createAnimation(*curve, WebAnimation::TargetPropertyOpacity)));
     98     int animationId = floatAnimation->id();
     99     ASSERT_TRUE(m_platformLayer->addAnimation(floatAnimation.leakPtr()));
    100 
    101     ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
    102 
    103     m_graphicsLayer->setPreserves3D(true);
    104 
    105     m_platformLayer = m_graphicsLayer->platformLayer();
    106     ASSERT_TRUE(m_platformLayer);
    107 
    108     ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
    109     m_platformLayer->removeAnimation(animationId);
    110     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
    111 
    112     m_graphicsLayer->setPreserves3D(false);
    113 
    114     m_platformLayer = m_graphicsLayer->platformLayer();
    115     ASSERT_TRUE(m_platformLayer);
    116 
    117     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
    118 }
    119 
    120 class FakeScrollableArea : public ScrollableArea {
    121 public:
    122     virtual bool isActive() const OVERRIDE { return false; }
    123     virtual int scrollSize(ScrollbarOrientation) const OVERRIDE { return 100; }
    124     virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
    125     virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
    126     virtual int visibleWidth() const OVERRIDE { return 10; }
    127     virtual int visibleHeight() const OVERRIDE { return 10; }
    128     virtual IntSize contentsSize() const OVERRIDE { return IntSize(100, 100); }
    129     virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
    130     virtual ScrollableArea* enclosingScrollableArea() const OVERRIDE { return 0; }
    131     virtual IntRect scrollableAreaBoundingBox() const OVERRIDE { return IntRect(); }
    132     virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE { }
    133     virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
    134     virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE { return true; }
    135     virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE { return false; }
    136     virtual int pageStep(ScrollbarOrientation) const OVERRIDE { return 0; }
    137     virtual IntPoint minimumScrollPosition() const OVERRIDE { return IntPoint(); }
    138     virtual IntPoint maximumScrollPosition() const OVERRIDE
    139     {
    140         return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
    141     }
    142 
    143     virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
    144     virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
    145 
    146 private:
    147     IntPoint m_scrollPosition;
    148 };
    149 
    150 TEST_F(GraphicsLayerTest, applyScrollToScrollableArea)
    151 {
    152     FakeScrollableArea scrollableArea;
    153     m_graphicsLayer->setScrollableArea(&scrollableArea, false);
    154 
    155     WebPoint scrollPosition(7, 9);
    156     m_platformLayer->setScrollPosition(scrollPosition);
    157     m_graphicsLayer->didScroll();
    158 
    159     EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));
    160 }
    161 
    162 TEST_F(GraphicsLayerTest, DISABLED_setContentsToSolidColor)
    163 {
    164     m_graphicsLayer->setContentsToSolidColor(Color::transparent);
    165     EXPECT_FALSE(m_graphicsLayer->contentsLayer());
    166 
    167     m_graphicsLayer->setContentsToSolidColor(Color::white);
    168     EXPECT_TRUE(m_graphicsLayer->contentsLayer());
    169 
    170     m_graphicsLayer->setContentsToSolidColor(Color());
    171     EXPECT_FALSE(m_graphicsLayer->contentsLayer());
    172 }
    173 
    174 } // namespace
    175