2 * Copyright (C) 2012 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.
27 #include "GraphicsLayerChromium.h"
29 #include "CompositorFakeWebGraphicsContext3D.h"
30 #include "GraphicsLayer.h"
31 #include "Matrix3DTransformOperation.h"
32 #include "RotateTransformOperation.h"
33 #include "ScrollableArea.h"
34 #include "TranslateTransformOperation.h"
35 #include "WebLayerTreeViewTestCommon.h"
36 #include <gtest/gtest.h>
37 #include <public/Platform.h>
38 #include <public/WebFloatAnimationCurve.h>
39 #include <public/WebGraphicsContext3D.h>
40 #include <public/WebLayerTreeView.h>
41 #include <public/WebTransformationMatrix.h>
42 #include <public/WebUnitTestSupport.h>
43 #include <wtf/PassOwnPtr.h>
45 using namespace WebCore;
46 using namespace WebKit;
50 class MockGraphicsLayerClient : public GraphicsLayerClient {
52 virtual void notifyAnimationStarted(const GraphicsLayer*, double time) OVERRIDE { }
53 virtual void notifyFlushRequired(const GraphicsLayer*) OVERRIDE { }
54 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE { }
55 virtual float deviceScaleFactor() const OVERRIDE { return 2; }
58 class GraphicsLayerChromiumTest : public testing::Test {
60 GraphicsLayerChromiumTest()
62 Platform::current()->compositorSupport()->initialize(0);
63 m_graphicsLayer = adoptPtr(new GraphicsLayerChromium(&m_client));
64 m_platformLayer = m_graphicsLayer->platformLayer();
65 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
66 ASSERT(m_layerTreeView);
67 m_layerTreeView->setRootLayer(*m_platformLayer);
68 m_layerTreeView->setViewportSize(WebSize(1, 1), WebSize(1, 1));
71 virtual ~GraphicsLayerChromiumTest()
73 m_graphicsLayer.clear();
74 m_layerTreeView.clear();
75 Platform::current()->compositorSupport()->shutdown();
79 static void expectTranslateX(double translateX, const WebTransformationMatrix& matrix)
81 EXPECT_FLOAT_EQ(translateX, matrix.m41());
84 WebLayer* m_platformLayer;
85 OwnPtr<GraphicsLayerChromium> m_graphicsLayer;
88 MockWebLayerTreeViewClient m_layerTreeViewClient;
89 OwnPtr<WebLayerTreeView> m_layerTreeView;
90 MockGraphicsLayerClient m_client;
93 TEST_F(GraphicsLayerChromiumTest, updateLayerPreserves3DWithAnimations)
95 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
97 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(Platform::current()->compositorSupport()->createFloatAnimationCurve());
98 curve->add(WebFloatKeyframe(0.0, 0.0));
99 OwnPtr<WebAnimation> floatAnimation(adoptPtr(Platform::current()->compositorSupport()->createAnimation(*curve, WebAnimation::TargetPropertyOpacity)));
100 int animationId = floatAnimation->id();
101 ASSERT_TRUE(m_platformLayer->addAnimation(floatAnimation.get()));
103 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
105 m_graphicsLayer->setPreserves3D(true);
107 m_platformLayer = m_graphicsLayer->platformLayer();
108 ASSERT_TRUE(m_platformLayer);
110 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
111 m_platformLayer->removeAnimation(animationId);
112 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
114 m_graphicsLayer->setPreserves3D(false);
116 m_platformLayer = m_graphicsLayer->platformLayer();
117 ASSERT_TRUE(m_platformLayer);
119 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
122 class FakeScrollableArea : public ScrollableArea {
124 virtual bool isActive() const OVERRIDE { return false; }
125 virtual int scrollSize(ScrollbarOrientation) const OVERRIDE { return 100; }
126 virtual int scrollPosition(Scrollbar*) const OVERRIDE { return 0; }
127 virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
128 virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
129 virtual int visibleWidth() const OVERRIDE { return 10; }
130 virtual int visibleHeight() const OVERRIDE { return 10; }
131 virtual IntSize contentsSize() const OVERRIDE { return IntSize(100, 100); }
132 virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
133 virtual ScrollableArea* enclosingScrollableArea() const OVERRIDE { return 0; }
134 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE { return IntRect(); }
135 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE { }
136 virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
138 virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
139 virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
142 IntPoint m_scrollPosition;
145 TEST_F(GraphicsLayerChromiumTest, applyScrollToScrollableArea)
147 FakeScrollableArea scrollableArea;
148 m_graphicsLayer->setScrollableArea(&scrollableArea);
150 WebPoint scrollPosition(7, 9);
151 m_platformLayer->setScrollPosition(scrollPosition);
153 EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));