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