Unreviewed, rolling out r126169.
[WebKit-https.git] / Source / WebKit / chromium / tests / WebLayerTest.cpp
1 /*
2  * Copyright (C) 2011 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 #include <public/WebLayer.h>
27
28 #include "CompositorFakeWebGraphicsContext3D.h"
29 #include "WebLayerImpl.h"
30 #include <public/WebCompositor.h>
31 #include <public/WebContentLayer.h>
32 #include <public/WebContentLayerClient.h>
33 #include <public/WebExternalTextureLayer.h>
34 #include <public/WebFloatPoint.h>
35 #include <public/WebFloatRect.h>
36 #include <public/WebLayerTreeView.h>
37 #include <public/WebLayerTreeViewClient.h>
38 #include <public/WebRect.h>
39 #include <public/WebSize.h>
40
41 #include <gmock/gmock.h>
42
43 using namespace WebKit;
44 using testing::AnyNumber;
45 using testing::AtLeast;
46 using testing::Mock;
47 using testing::Test;
48 using testing::_;
49
50 namespace {
51
52 class MockWebLayerTreeViewClient : public WebLayerTreeViewClient {
53 public:
54     MOCK_METHOD0(scheduleComposite, void());
55
56     virtual void updateAnimations(double frameBeginTime) { }
57     virtual void didBeginFrame() { }
58     virtual void layout() { }
59     virtual void applyScrollAndScale(const WebSize& scrollDelta, float scaleFactor) { }
60     virtual WebGraphicsContext3D* createContext3D() { return CompositorFakeWebGraphicsContext3D::create(WebGraphicsContext3D::Attributes()).leakPtr(); }
61     virtual void didRebindGraphicsContext(bool success) { }
62     virtual WebCompositorOutputSurface* createOutputSurface() { return 0; }
63     virtual void didRecreateOutputSurface(bool success) { }
64     virtual void willCommit() { }
65     virtual void didCommitAndDrawFrame() { }
66     virtual void didCompleteSwapBuffers() { }
67 };
68
69 class MockWebContentLayerClient : public WebContentLayerClient {
70 public:
71     MOCK_METHOD3(paintContents, void(WebCanvas*, const WebRect& clip, WebFloatRect& opaque));
72 };
73
74 class WebLayerTest : public Test {
75 public:
76     virtual void SetUp()
77     {
78         // Initialize without threading support.
79         WebKit::WebCompositor::initialize(0);
80         m_rootLayer = adoptPtr(WebLayer::create());
81         EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
82         EXPECT_TRUE(m_view.initialize(&m_client, *m_rootLayer, WebLayerTreeView::Settings()));
83         Mock::VerifyAndClearExpectations(&m_client);
84     }
85
86     virtual void TearDown()
87     {
88         // We may get any number of scheduleComposite calls during shutdown.
89         EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
90         m_view.setRootLayer(0);
91         m_rootLayer.clear();
92         m_view.reset();
93         WebKit::WebCompositor::shutdown();
94     }
95
96 protected:
97     MockWebLayerTreeViewClient m_client;
98     OwnPtr<WebLayer> m_rootLayer;
99     WebLayerTreeView m_view;
100 };
101
102 // Tests that the client gets called to ask for a composite if we change the
103 // fields.
104 TEST_F(WebLayerTest, Client)
105 {
106     // Base layer.
107     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
108     OwnPtr<WebLayer> layer = adoptPtr(WebLayer::create());
109     m_rootLayer->addChild(layer.get());
110     Mock::VerifyAndClearExpectations(&m_client);
111
112     WebFloatPoint point(3, 4);
113     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
114     layer->setAnchorPoint(point);
115     Mock::VerifyAndClearExpectations(&m_client);
116     EXPECT_EQ(point, layer->anchorPoint());
117
118     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
119     float anchorZ = 5;
120     layer->setAnchorPointZ(anchorZ);
121     Mock::VerifyAndClearExpectations(&m_client);
122     EXPECT_EQ(anchorZ, layer->anchorPointZ());
123
124     WebSize size(7, 8);
125     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
126     layer->setBounds(size);
127     Mock::VerifyAndClearExpectations(&m_client);
128     EXPECT_EQ(size, layer->bounds());
129
130     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
131     layer->setMasksToBounds(true);
132     Mock::VerifyAndClearExpectations(&m_client);
133     EXPECT_TRUE(layer->masksToBounds());
134
135     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
136     OwnPtr<WebLayer> otherLayer = adoptPtr(WebLayer::create());
137     m_rootLayer->addChild(otherLayer.get());
138     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
139     layer->setMaskLayer(otherLayer.get());
140     Mock::VerifyAndClearExpectations(&m_client);
141
142     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
143     float opacity = 0.123f;
144     layer->setOpacity(opacity);
145     Mock::VerifyAndClearExpectations(&m_client);
146     EXPECT_EQ(opacity, layer->opacity());
147
148     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
149     layer->setOpaque(true);
150     Mock::VerifyAndClearExpectations(&m_client);
151     EXPECT_TRUE(layer->opaque());
152
153     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
154     layer->setPosition(point);
155     Mock::VerifyAndClearExpectations(&m_client);
156     EXPECT_EQ(point, layer->position());
157
158     // Texture layer.
159     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
160     OwnPtr<WebExternalTextureLayer> textureLayer = adoptPtr(WebExternalTextureLayer::create());
161     m_rootLayer->addChild(textureLayer->layer());
162     Mock::VerifyAndClearExpectations(&m_client);
163
164     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
165     textureLayer->setTextureId(3);
166     Mock::VerifyAndClearExpectations(&m_client);
167
168     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
169     textureLayer->setFlipped(true);
170     Mock::VerifyAndClearExpectations(&m_client);
171
172     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
173     WebFloatRect uvRect(0.1f, 0.1f, 0.9f, 0.9f);
174     textureLayer->setUVRect(uvRect);
175     Mock::VerifyAndClearExpectations(&m_client);
176
177
178     // Content layer.
179     MockWebContentLayerClient contentClient;
180     EXPECT_CALL(contentClient, paintContents(_, _, _)).Times(AnyNumber());
181     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
182     OwnPtr<WebContentLayer> contentLayer = adoptPtr(WebContentLayer::create(&contentClient));
183     m_rootLayer->addChild(contentLayer->layer());
184     Mock::VerifyAndClearExpectations(&m_client);
185
186     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
187     contentLayer->layer()->setDrawsContent(false);
188     Mock::VerifyAndClearExpectations(&m_client);
189     EXPECT_FALSE(contentLayer->layer()->drawsContent());
190 }
191
192 }