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