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 "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"
41
42#include <gtest/gtest.h>
43
44using namespace blink;
45
46namespace {
47
48class MockGraphicsLayerClient : public GraphicsLayerClient {
49public:
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(); }
53};
54
55class GraphicsLayerForTesting : public GraphicsLayer {
56public:
57    explicit GraphicsLayerForTesting(GraphicsLayerClient* client)
58        : GraphicsLayer(client) { };
59
60    virtual WebLayer* contentsLayer() const { return GraphicsLayer::contentsLayer(); }
61};
62
63class GraphicsLayerTest : public testing::Test {
64public:
65    GraphicsLayerTest()
66    {
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));
79    }
80
81    virtual ~GraphicsLayerTest()
82    {
83        m_graphicsLayer.clear();
84        m_layerTreeView.clear();
85    }
86
87protected:
88    WebLayer* m_platformLayer;
89    OwnPtr<GraphicsLayerForTesting> m_graphicsLayer;
90    OwnPtr<GraphicsLayerForTesting> m_clipLayer;
91
92private:
93    OwnPtr<WebLayerTreeView> m_layerTreeView;
94    MockGraphicsLayerClient m_client;
95};
96
97TEST_F(GraphicsLayerTest, updateLayerShouldFlattenTransformWithAnimations)
98{
99    ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
100
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()));
106
107    ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
108
109    m_graphicsLayer->setShouldFlattenTransform(false);
110
111    m_platformLayer = m_graphicsLayer->platformLayer();
112    ASSERT_TRUE(m_platformLayer);
113
114    ASSERT_TRUE(m_platformLayer->hasActiveAnimation());
115    m_platformLayer->removeAnimation(animationId);
116    ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
117
118    m_graphicsLayer->setShouldFlattenTransform(true);
119
120    m_platformLayer = m_graphicsLayer->platformLayer();
121    ASSERT_TRUE(m_platformLayer);
122
123    ASSERT_FALSE(m_platformLayer->hasActiveAnimation());
124}
125
126class FakeScrollableArea : public ScrollableArea {
127public:
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
144    {
145        return IntPoint(contentsSize().width() - visibleWidth(), contentsSize().height() - visibleHeight());
146    }
147
148    virtual void setScrollOffset(const IntPoint& scrollOffset) OVERRIDE { m_scrollPosition = scrollOffset; }
149    virtual IntPoint scrollPosition() const OVERRIDE { return m_scrollPosition; }
150
151private:
152    IntPoint m_scrollPosition;
153};
154
155TEST_F(GraphicsLayerTest, applyScrollToScrollableArea)
156{
157    FakeScrollableArea scrollableArea;
158    m_graphicsLayer->setScrollableArea(&scrollableArea, false);
159
160    WebPoint scrollPosition(7, 9);
161    m_platformLayer->setScrollPosition(scrollPosition);
162    m_graphicsLayer->didScroll();
163
164    EXPECT_EQ(scrollPosition, WebPoint(scrollableArea.scrollPosition()));
165}
166
167} // namespace
168