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 "platform/graphics/GraphicsLayer.h"
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"
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"
44 using namespace WebCore;
45 using namespace blink;
49 class MockGraphicsLayerClient : public GraphicsLayerClient {
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(); }
56 class GraphicsLayerForTesting : public GraphicsLayer {
58 explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
59 : GraphicsLayer(client) { };
61 virtual blink::WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
64 class GraphicsLayerTest : public testing::Test {
68 m_clipLayer = adoptPtr(new GraphicsLayerForTesting(&m_client));
69 m_graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&m_client));
70 m_clipLayer->addChild(m_graphicsLayer.get());
71 m_graphicsLayer->platformLayer()->setScrollClipLayer(
72 m_clipLayer->platformLayer());
73 m_platformLayer = m_graphicsLayer->platformLayer();
74 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
75 ASSERT(m_layerTreeView);
76 m_layerTreeView->setRootLayer(*m_clipLayer->platformLayer());
77 m_layerTreeView->registerViewportLayers(
78 m_clipLayer->platformLayer(), m_graphicsLayer->platformLayer(), 0);
79 m_layerTreeView->setViewportSize(WebSize(1, 1), WebSize(1, 1));
82 virtual ~GraphicsLayerTest()
84 m_graphicsLayer.clear();
85 m_layerTreeView.clear();
89 WebLayer* m_platformLayer;
90 OwnPtr<GraphicsLayerForTesting> m_graphicsLayer;
91 OwnPtr<GraphicsLayerForTesting> m_clipLayer;
94 OwnPtr<WebLayerTreeView> m_layerTreeView;
95 MockGraphicsLayerClient m_client;
98 TEST_F(GraphicsLayerTest, updateLayerShouldFlattenTransformWithAnimations)
100 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
102 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(Platform::current()->compositorSupport()->createFloatAnimationCurve());
103 curve->add(WebFloatKeyframe(0.0, 0.0));
104 OwnPtr<WebAnimation> floatAnimation(adoptPtr(Platform::current()->compositorSupport()->createAnimation(*curve, WebAnimation::TargetPropertyOpacity)));
105 int animationId = floatAnimation->id();
106 ASSERT_TRUE(m_platformLayer->addAnimation(floatAnimation.leakPtr()));
108 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
110 m_graphicsLayer->setShouldFlattenTransform(false);
112 m_platformLayer = m_graphicsLayer->platformLayer();
113 ASSERT_TRUE(m_platformLayer);
115 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
116 m_platformLayer->removeAnimation(animationId);
117 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
119 m_graphicsLayer->setShouldFlattenTransform(true);
121 m_platformLayer = m_graphicsLayer->platformLayer();
122 ASSERT_TRUE(m_platformLayer);
124 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
127 class FakeScrollableArea : public ScrollableArea {
129 virtual bool isActive() const OVERRIDE { return false; }
130 virtual int scrollSize(ScrollbarOrientation) const OVERRIDE { return 100; }
131 virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
132 virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
133 virtual int visibleWidth() const OVERRIDE { return 10; }
134 virtual int visibleHeight() const OVERRIDE { return 10; }
135 virtual IntSize contentsSize() const OVERRIDE { return IntSize(100, 100); }
136 virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
137 virtual ScrollableArea* enclosingScrollableArea() const OVERRIDE { return 0; }
138 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE { return IntRect(); }
139 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE { }
140 virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
141 virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE { return true; }
142 virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE { return false; }
143 virtual int pageStep(ScrollbarOrientation) const OVERRIDE { return 0; }
144 virtual IntPoint minimumScrollPosition() const OVERRIDE { return IntPoint(); }
145 virtual IntPoint maximumScrollPosition() const OVERRIDE
147 return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
150 virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
151 virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
154 IntPoint m_scrollPosition;
157 TEST_F(GraphicsLayerTest, applyScrollToScrollableArea)
159 FakeScrollableArea scrollableArea;
160 m_graphicsLayer->setScrollableArea(&scrollableArea, false);
162 WebPoint scrollPosition(7, 9);
163 m_platformLayer->setScrollPosition(scrollPosition);
164 m_graphicsLayer->didScroll();
166 EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));
169 TEST_F(GraphicsLayerTest, DISABLED_setContentsToSolidColor)
171 m_graphicsLayer->setContentsToSolidColor(Color::transparent);
172 EXPECT_FALSE(m_graphicsLayer->contentsLayer());
174 m_graphicsLayer->setContentsToSolidColor(Color::white);
175 EXPECT_TRUE(m_graphicsLayer->contentsLayer());
177 m_graphicsLayer->setContentsToSolidColor(Color());
178 EXPECT_FALSE(m_graphicsLayer->contentsLayer());