Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / GraphicsLayerTest.cpp
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_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));
80     }
81
82     virtual ~GraphicsLayerTest()
83     {
84         m_graphicsLayer.clear();
85         m_layerTreeView.clear();
86     }
87
88 protected:
89     WebLayer* m_platformLayer;
90     OwnPtr<GraphicsLayerForTesting> m_graphicsLayer;
91     OwnPtr<GraphicsLayerForTesting> m_clipLayer;
92
93 private:
94     OwnPtr<WebLayerTreeView> m_layerTreeView;
95     MockGraphicsLayerClient m_client;
96 };
97
98 TEST_F(GraphicsLayerTest, updateLayerShouldFlattenTransformWithAnimations)
99 {
100     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
101
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()));
107
108     ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
109
110     m_graphicsLayer->setShouldFlattenTransform(false);
111
112     m_platformLayer = m_graphicsLayer->platformLayer();
113     ASSERT_TRUE(m_platformLayer);
114
115     ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
116     m_platformLayer->removeAnimation(animationId);
117     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
118
119     m_graphicsLayer->setShouldFlattenTransform(true);
120
121     m_platformLayer = m_graphicsLayer->platformLayer();
122     ASSERT_TRUE(m_platformLayer);
123
124     ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
125 }
126
127 class FakeScrollableArea : public ScrollableArea {
128 public:
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
146     {
147         return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
148     }
149
150     virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
151     virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
152
153 private:
154     IntPoint m_scrollPosition;
155 };
156
157 TEST_F(GraphicsLayerTest, applyScrollToScrollableArea)
158 {
159     FakeScrollableArea scrollableArea;
160     m_graphicsLayer->setScrollableArea(&scrollableArea, false);
161
162     WebPoint scrollPosition(7, 9);
163     m_platformLayer->setScrollPosition(scrollPosition);
164     m_graphicsLayer->didScroll();
165
166     EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));
167 }
168
169 TEST_F(GraphicsLayerTest, DISABLED_setContentsToSolidColor)
170 {
171     m_graphicsLayer->setContentsToSolidColor(Color::transparent);
172     EXPECT_FALSE(m_graphicsLayer->contentsLayer());
173
174     m_graphicsLayer->setContentsToSolidColor(Color::white);
175     EXPECT_TRUE(m_graphicsLayer->contentsLayer());
176
177     m_graphicsLayer->setContentsToSolidColor(Color());
178     EXPECT_FALSE(m_graphicsLayer->contentsLayer());
179 }
180
181 } // namespace