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 "public/platform/Platform.h"
34 #include "public/platform/WebCompositorSupport.h"
35 #include "public/platform/WebFloatAnimationCurve.h"
36 #include "public/platform/WebGraphicsContext3D.h"
37 #include "public/platform/WebLayer.h"
38 #include "public/platform/WebLayerTreeView.h"
39 #include "public/platform/WebUnitTestSupport.h"
40 #include "wtf/PassOwnPtr.h"
42 #include <gtest/gtest.h>
44 using namespace blink;
48 class MockGraphicsLayerClient : public GraphicsLayerClient {
50 virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE { }
51 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) OVERRIDE { }
52 virtual String debugName(const GraphicsLayer*) OVERRIDE { return String(); }
55 class GraphicsLayerForTesting : public GraphicsLayer {
57 explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
58 : GraphicsLayer(client) { };
60 virtual WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
63 class GraphicsLayerTest : public testing::Test {
67 m_clipLayer = adoptPtr(new GraphicsLayerForTesting(&m_client));
68 m_graphicsLayer = adoptPtr(new GraphicsLayerForTesting(&m_client));
69 m_clipLayer->addChild(m_graphicsLayer.get());
70 m_graphicsLayer->platformLayer()->setScrollClipLayer(
71 m_clipLayer->platformLayer());
72 m_platformLayer = m_graphicsLayer->platformLayer();
73 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting());
74 ASSERT(m_layerTreeView);
75 m_layerTreeView->setRootLayer(*m_clipLayer->platformLayer());
76 m_layerTreeView->registerViewportLayers(
77 m_clipLayer->platformLayer(), m_graphicsLayer->platformLayer(), 0);
78 m_layerTreeView->setViewportSize(WebSize(1, 1));
81 virtual ~GraphicsLayerTest()
83 m_graphicsLayer.clear();
84 m_layerTreeView.clear();
88 WebLayer* m_platformLayer;
89 OwnPtr<GraphicsLayerForTesting> m_graphicsLayer;
90 OwnPtr<GraphicsLayerForTesting> m_clipLayer;
93 OwnPtr<WebLayerTreeView> m_layerTreeView;
94 MockGraphicsLayerClient m_client;
97 TEST_F(GraphicsLayerTest, updateLayerShouldFlattenTransformWithAnimations)
99 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
101 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(Platform::current()->compositorSupport()->createFloatAnimationCurve());
102 curve->add(WebFloatKeyframe(0.0, 0.0));
103 OwnPtr<WebCompositorAnimation> floatAnimation(adoptPtr(Platform::current()->compositorSupport()->createAnimation(*curve, WebCompositorAnimation::TargetPropertyOpacity)));
104 int animationId = floatAnimation->id();
105 ASSERT_TRUE(m_platformLayer->addAnimation(floatAnimation.leakPtr()));
107 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
109 m_graphicsLayer->setShouldFlattenTransform(false);
111 m_platformLayer = m_graphicsLayer->platformLayer();
112 ASSERT_TRUE(m_platformLayer);
114 ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
115 m_platformLayer->removeAnimation(animationId);
116 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
118 m_graphicsLayer->setShouldFlattenTransform(true);
120 m_platformLayer = m_graphicsLayer->platformLayer();
121 ASSERT_TRUE(m_platformLayer);
123 ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
126 class FakeScrollableArea : public ScrollableArea {
128 virtual bool isActive() const OVERRIDE { return false; }
129 virtual int scrollSize(ScrollbarOrientation) const OVERRIDE { return 100; }
130 virtual bool isScrollCornerVisible() const OVERRIDE { return false; }
131 virtual IntRect scrollCornerRect() const OVERRIDE { return IntRect(); }
132 virtual int visibleWidth() const OVERRIDE { return 10; }
133 virtual int visibleHeight() const OVERRIDE { return 10; }
134 virtual IntSize contentsSize() const OVERRIDE { return IntSize(100, 100); }
135 virtual bool scrollbarsCanBeActive() const OVERRIDE { return false; }
136 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE { return IntRect(); }
137 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE { }
138 virtual void invalidateScrollCornerRect(const IntRect&) OVERRIDE { }
139 virtual bool userInputScrollable(ScrollbarOrientation) const OVERRIDE { return true; }
140 virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE { return false; }
141 virtual int pageStep(ScrollbarOrientation) const OVERRIDE { return 0; }
142 virtual IntPoint minimumScrollPosition() const OVERRIDE { return IntPoint(); }
143 virtual IntPoint maximumScrollPosition() const OVERRIDE
145 return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
148 virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
149 virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
152 IntPoint m_scrollPosition;
155 TEST_F(GraphicsLayerTest, applyScrollToScrollableArea)
157 FakeScrollableArea scrollableArea;
158 m_graphicsLayer->setScrollableArea(&scrollableArea, false);
160 WebPoint scrollPosition(7, 9);
161 m_platformLayer->setScrollPosition(scrollPosition);
162 m_graphicsLayer->didScroll();
164 EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));