[chromium] Use TextureLayerChromium for WebGL content instead of a dedicated layer...
[WebKit.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 "platform/WebLayer.h"
27
28 #include "CompositorFakeWebGraphicsContext3D.h"
29 #include "WebCompositor.h"
30 #include "platform/WebContentLayer.h"
31 #include "platform/WebContentLayerClient.h"
32 #include "platform/WebExternalTextureLayer.h"
33 #include "platform/WebFloatPoint.h"
34 #include "platform/WebFloatRect.h"
35 #include "platform/WebLayerTreeView.h"
36 #include "platform/WebLayerTreeViewClient.h"
37 #include "platform/WebRect.h"
38 #include "platform/WebSize.h"
39
40 #include <gmock/gmock.h>
41
42 using namespace WebKit;
43 using testing::AnyNumber;
44 using testing::AtLeast;
45 using testing::Mock;
46 using testing::Test;
47 using testing::_;
48
49 namespace {
50
51 class MockWebLayerTreeViewClient : public WebLayerTreeViewClient {
52 public:
53     MOCK_METHOD0(scheduleComposite, void());
54
55     virtual void updateAnimations(double frameBeginTime) { }
56     virtual void layout() { }
57     virtual void applyScrollAndScale(const WebSize& scrollDelta, float scaleFactor) { }
58     virtual WebGraphicsContext3D* createContext3D() { return CompositorFakeWebGraphicsContext3D::create(WebGraphicsContext3D::Attributes()).leakPtr(); }
59     virtual void didRebindGraphicsContext(bool success) { }
60     virtual void didCommitAndDrawFrame() { }
61     virtual void didCompleteSwapBuffers() { }
62 };
63
64 class MockWebContentLayerClient : public WebContentLayerClient {
65 public:
66     MOCK_METHOD2(paintContents, void(WebCanvas*, const WebRect& clip));
67 };
68
69 class WebLayerTest : public Test {
70 public:
71     virtual void SetUp()
72     {
73         // Initialize without threading support.
74         WebKit::WebCompositor::initialize(0);
75         m_rootLayer = WebLayer::create();
76         EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
77         EXPECT_TRUE(m_view.initialize(&m_client, m_rootLayer, WebLayerTreeView::Settings()));
78         Mock::VerifyAndClearExpectations(&m_client);
79     }
80
81     virtual void TearDown()
82     {
83         // We may get any number of scheduleComposite calls during shutdown.
84         EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
85         m_view.setRootLayer(0);
86         m_rootLayer.reset();
87         m_view.reset();
88         WebKit::WebCompositor::shutdown();
89     }
90
91 protected:
92     MockWebLayerTreeViewClient m_client;
93     WebLayer m_rootLayer;
94     WebLayerTreeView m_view;
95 };
96
97 // Tests that the client gets called to ask for a composite if we change the
98 // fields.
99 TEST_F(WebLayerTest, Client)
100 {
101     // Base layer.
102     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
103     WebLayer layer = WebLayer::create();
104     m_rootLayer.addChild(layer);
105     Mock::VerifyAndClearExpectations(&m_client);
106
107     WebFloatPoint point(3, 4);
108     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
109     layer.setAnchorPoint(point);
110     Mock::VerifyAndClearExpectations(&m_client);
111     EXPECT_EQ(point, layer.anchorPoint());
112
113     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
114     float anchorZ = 5;
115     layer.setAnchorPointZ(anchorZ);
116     Mock::VerifyAndClearExpectations(&m_client);
117     EXPECT_EQ(anchorZ, layer.anchorPointZ());
118
119     WebSize size(7, 8);
120     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
121     layer.setBounds(size);
122     Mock::VerifyAndClearExpectations(&m_client);
123     EXPECT_EQ(size, layer.bounds());
124
125     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
126     layer.setMasksToBounds(true);
127     Mock::VerifyAndClearExpectations(&m_client);
128     EXPECT_TRUE(layer.masksToBounds());
129
130     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
131     WebLayer otherLayer = WebLayer::create();
132     m_rootLayer.addChild(otherLayer);
133     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
134     layer.setMaskLayer(otherLayer);
135     Mock::VerifyAndClearExpectations(&m_client);
136     EXPECT_EQ(otherLayer, layer.maskLayer());
137
138     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
139     float opacity = 0.123f;
140     layer.setOpacity(opacity);
141     Mock::VerifyAndClearExpectations(&m_client);
142     EXPECT_EQ(opacity, layer.opacity());
143
144     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
145     layer.setOpaque(true);
146     Mock::VerifyAndClearExpectations(&m_client);
147     EXPECT_TRUE(layer.opaque());
148
149     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
150     layer.setPosition(point);
151     Mock::VerifyAndClearExpectations(&m_client);
152     EXPECT_EQ(point, layer.position());
153
154     // Texture layer.
155     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
156     WebExternalTextureLayer textureLayer = WebExternalTextureLayer::create();
157     m_rootLayer.addChild(textureLayer);
158     Mock::VerifyAndClearExpectations(&m_client);
159
160     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
161     textureLayer.setTextureId(3);
162     Mock::VerifyAndClearExpectations(&m_client);
163
164     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
165     textureLayer.setFlipped(true);
166     Mock::VerifyAndClearExpectations(&m_client);
167
168     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
169     WebFloatRect uvRect(0.1f, 0.1f, 0.9f, 0.9f);
170     textureLayer.setUVRect(uvRect);
171     Mock::VerifyAndClearExpectations(&m_client);
172
173
174     // Content layer.
175     MockWebContentLayerClient contentClient;
176     EXPECT_CALL(contentClient, paintContents(_, _)).Times(AnyNumber());
177     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
178     WebContentLayer contentLayer = WebContentLayer::create(&contentClient);
179     m_rootLayer.addChild(contentLayer);
180     Mock::VerifyAndClearExpectations(&m_client);
181
182     EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
183     contentLayer.setDrawsContent(false);
184     Mock::VerifyAndClearExpectations(&m_client);
185     EXPECT_FALSE(contentLayer.drawsContent());
186 }
187
188 TEST_F(WebLayerTest, Hierarchy)
189 {
190     EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
191     WebLayer layer1 = WebLayer::create();
192     WebLayer layer2 = WebLayer::create();
193
194     EXPECT_TRUE(layer1.parent().isNull());
195     EXPECT_TRUE(layer2.parent().isNull());
196
197     layer1.addChild(layer2);
198     EXPECT_TRUE(layer1.parent().isNull());
199     EXPECT_EQ(layer1, layer2.parent());
200
201     layer2.removeFromParent();
202     EXPECT_TRUE(layer2.parent().isNull());
203
204     layer1.addChild(layer2);
205     EXPECT_EQ(layer1, layer2.parent());
206     layer1.removeAllChildren();
207     EXPECT_TRUE(layer2.parent().isNull());
208
209     MockWebContentLayerClient contentClient;
210     EXPECT_CALL(contentClient, paintContents(_, _)).Times(AnyNumber());
211     WebContentLayer contentLayer = WebContentLayer::create(&contentClient);
212     WebExternalTextureLayer textureLayer = WebExternalTextureLayer::create();
213
214     textureLayer.addChild(contentLayer);
215     contentLayer.addChild(layer1);
216     layer1.addChild(layer2);
217
218     // Release reference on all layers, checking that destruction (which may
219     // generate calls to the client) doesn't crash.
220     layer2.reset();
221     layer1.reset();
222     contentLayer.reset();
223     textureLayer.reset();
224 }
225
226 }