2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include <public/WebLayer.h>
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>
41 #include <gmock/gmock.h>
43 using namespace WebKit;
44 using testing::AnyNumber;
45 using testing::AtLeast;
52 class MockWebLayerTreeViewClient : public WebLayerTreeViewClient {
54 MOCK_METHOD0(scheduleComposite, void());
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() { }
69 class MockWebContentLayerClient : public WebContentLayerClient {
71 MOCK_METHOD3(paintContents, void(WebCanvas*, const WebRect& clip, WebFloatRect& opaque));
74 class WebLayerTest : public Test {
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);
86 virtual void TearDown()
88 // We may get any number of scheduleComposite calls during shutdown.
89 EXPECT_CALL(m_client, scheduleComposite()).Times(AnyNumber());
90 m_view.setRootLayer(0);
93 WebKit::WebCompositor::shutdown();
97 MockWebLayerTreeViewClient m_client;
98 OwnPtr<WebLayer> m_rootLayer;
99 WebLayerTreeView m_view;
102 // Tests that the client gets called to ask for a composite if we change the
104 TEST_F(WebLayerTest, Client)
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);
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());
118 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
120 layer->setAnchorPointZ(anchorZ);
121 Mock::VerifyAndClearExpectations(&m_client);
122 EXPECT_EQ(anchorZ, layer->anchorPointZ());
125 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
126 layer->setBounds(size);
127 Mock::VerifyAndClearExpectations(&m_client);
128 EXPECT_EQ(size, layer->bounds());
130 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
131 layer->setMasksToBounds(true);
132 Mock::VerifyAndClearExpectations(&m_client);
133 EXPECT_TRUE(layer->masksToBounds());
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);
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());
148 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
149 layer->setOpaque(true);
150 Mock::VerifyAndClearExpectations(&m_client);
151 EXPECT_TRUE(layer->opaque());
153 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
154 layer->setPosition(point);
155 Mock::VerifyAndClearExpectations(&m_client);
156 EXPECT_EQ(point, layer->position());
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);
164 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
165 textureLayer->setTextureId(3);
166 Mock::VerifyAndClearExpectations(&m_client);
168 EXPECT_CALL(m_client, scheduleComposite()).Times(AtLeast(1));
169 textureLayer->setFlipped(true);
170 Mock::VerifyAndClearExpectations(&m_client);
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);
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);
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());