8dd255c8e26931cf373ea5af4381f9bf6abde891
[WebKit-https.git] / Source / WebKit / chromium / tests / ScrollingCoordinatorChromiumTest.cpp
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 "ScrollingCoordinator.h"
28
29 #include "CompositorFakeWebGraphicsContext3D.h"
30 #include "FrameTestHelpers.h"
31 #include "GraphicsLayerChromium.h"
32 #include "RenderLayerBacking.h"
33 #include "RenderLayerCompositor.h"
34 #include "RenderView.h"
35 #include "URLTestHelpers.h"
36 #include "WebCompositorInitializer.h"
37 #include "WebFrameClient.h"
38 #include "WebFrameImpl.h"
39 #include "WebSettings.h"
40 #include "WebViewClient.h"
41 #include "WebViewImpl.h"
42 #include <gtest/gtest.h>
43 #include <public/Platform.h>
44 #include <public/WebCompositorSupport.h>
45 #include <public/WebLayer.h>
46 #include <public/WebUnitTestSupport.h>
47
48 using namespace WebCore;
49 using namespace WebKit;
50
51 namespace {
52
53 class FakeWebViewClient : public WebViewClient {
54 public:
55     virtual WebCompositorOutputSurface* createOutputSurface() OVERRIDE
56     {
57         return Platform::current()->compositorSupport()->createOutputSurfaceFor3D(CompositorFakeWebGraphicsContext3D::create(WebGraphicsContext3D::Attributes()).leakPtr());
58     }
59
60     virtual void initializeLayerTreeView(WebLayerTreeViewClient* client, const WebLayer& rootLayer, const WebLayerTreeView::Settings& settings)
61     {
62         m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
63         ASSERT(m_layerTreeView);
64         m_layerTreeView->setRootLayer(rootLayer);
65     }
66
67     virtual WebLayerTreeView* layerTreeView()
68     {
69         return m_layerTreeView.get();
70     }
71
72 private:
73     OwnPtr<WebLayerTreeView> m_layerTreeView;
74 };
75
76 class MockWebFrameClient : public WebFrameClient {
77 };
78
79 class ScrollingCoordinatorChromiumTest : public testing::Test {
80 public:
81     ScrollingCoordinatorChromiumTest()
82         : m_baseURL("http://www.test.com/")
83     {
84         Platform::current()->compositorSupport()->initialize(0);
85
86         // We cannot reuse FrameTestHelpers::createWebViewAndLoad here because the compositing
87         // settings need to be set before the page is loaded.
88         m_webViewImpl = static_cast<WebViewImpl*>(WebView::create(&m_mockWebViewClient));
89         m_webViewImpl->settings()->setJavaScriptEnabled(true);
90         m_webViewImpl->settings()->setForceCompositingMode(true);
91         m_webViewImpl->settings()->setAcceleratedCompositingEnabled(true);
92         m_webViewImpl->settings()->setAcceleratedCompositingForFixedPositionEnabled(true);
93         m_webViewImpl->settings()->setAcceleratedCompositingForOverflowScrollEnabled(true);
94         m_webViewImpl->settings()->setAcceleratedCompositingForScrollableFramesEnabled(true);
95         m_webViewImpl->settings()->setCompositedScrollingForFramesEnabled(true);
96         m_webViewImpl->settings()->setFixedPositionCreatesStackingContext(true);
97         m_webViewImpl->initializeMainFrame(&m_mockWebFrameClient);
98         m_webViewImpl->resize(IntSize(320, 240));
99     }
100
101     virtual ~ScrollingCoordinatorChromiumTest()
102     {
103         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
104         m_webViewImpl->close();
105
106         Platform::current()->compositorSupport()->shutdown();
107     }
108
109     void navigateTo(const std::string& url)
110     {
111         FrameTestHelpers::loadFrame(m_webViewImpl->mainFrame(), url);
112         Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
113     }
114
115     void registerMockedHttpURLLoad(const std::string& fileName)
116     {
117         URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
118     }
119
120     WebLayer* getRootScrollLayer()
121     {
122         RenderLayerCompositor* compositor = m_webViewImpl->mainFrameImpl()->frame()->contentRenderer()->compositor();
123         ASSERT(compositor);
124         ASSERT(compositor->scrollLayer());
125
126         WebLayer* webScrollLayer = static_cast<WebLayer*>(compositor->scrollLayer()->platformLayer());
127         return webScrollLayer;
128     }
129
130 protected:
131     std::string m_baseURL;
132     MockWebFrameClient m_mockWebFrameClient;
133     FakeWebViewClient m_mockWebViewClient;
134     WebViewImpl* m_webViewImpl;
135 };
136
137 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingByDefault)
138 {
139     navigateTo("about:blank");
140
141     // Make sure the scrolling coordinator is active.
142     FrameView* frameView = m_webViewImpl->mainFrameImpl()->frameView();
143     Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
144     ASSERT_TRUE(page->scrollingCoordinator());
145     ASSERT_TRUE(page->scrollingCoordinator()->coordinatesScrollingForFrameView(frameView));
146
147     // Fast scrolling should be enabled by default.
148     WebLayer* rootScrollLayer = getRootScrollLayer();
149     ASSERT_TRUE(rootScrollLayer->scrollable());
150     ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
151     ASSERT_FALSE(rootScrollLayer->haveWheelEventHandlers());
152 }
153
154 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingForFixedPosition)
155 {
156     registerMockedHttpURLLoad("fixed-position.html");
157     navigateTo(m_baseURL + "fixed-position.html");
158
159     Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
160     ASSERT_TRUE(page->scrollingCoordinator()->supportsFixedPositionLayers());
161
162     // Fixed position should not fall back to main thread scrolling.
163     WebLayer* rootScrollLayer = getRootScrollLayer();
164     ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
165
166     // Verify the properties of the fixed position element starting from the RenderObject all the
167     // way to the WebLayer.
168     Element* fixedElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("fixed");
169     ASSERT(fixedElement);
170
171     RenderObject* renderer = fixedElement->renderer();
172     ASSERT_TRUE(renderer->isBoxModelObject());
173     ASSERT_TRUE(renderer->hasLayer());
174
175     RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
176     ASSERT_TRUE(layer->isComposited());
177
178     RenderLayerBacking* layerBacking = layer->backing();
179     WebLayer* webLayer = static_cast<WebLayer*>(layerBacking->graphicsLayer()->platformLayer());
180     ASSERT_TRUE(webLayer->fixedToContainerLayer());
181 }
182
183 TEST_F(ScrollingCoordinatorChromiumTest, nonFastScrollableRegion)
184 {
185     registerMockedHttpURLLoad("non-fast-scrollable.html");
186     navigateTo(m_baseURL + "non-fast-scrollable.html");
187
188     WebLayer* rootScrollLayer = getRootScrollLayer();
189     WebVector<WebRect> nonFastScrollableRegion = rootScrollLayer->nonFastScrollableRegion();
190
191     ASSERT_EQ(1u, nonFastScrollableRegion.size());
192     ASSERT_EQ(WebRect(8, 8, 10, 10), nonFastScrollableRegion[0]);
193 }
194
195 TEST_F(ScrollingCoordinatorChromiumTest, wheelEventHandler)
196 {
197     registerMockedHttpURLLoad("wheel-event-handler.html");
198     navigateTo(m_baseURL + "wheel-event-handler.html");
199
200     WebLayer* rootScrollLayer = getRootScrollLayer();
201     ASSERT_TRUE(rootScrollLayer->haveWheelEventHandlers());
202 }
203
204 TEST_F(ScrollingCoordinatorChromiumTest, clippedBodyTest)
205 {
206     registerMockedHttpURLLoad("clipped-body.html");
207     navigateTo(m_baseURL + "clipped-body.html");
208
209     WebLayer* rootScrollLayer = getRootScrollLayer();
210     ASSERT_EQ(0u, rootScrollLayer->nonFastScrollableRegion().size());
211 }
212
213 TEST_F(ScrollingCoordinatorChromiumTest, overflowScrolling)
214 {
215     registerMockedHttpURLLoad("overflow-scrolling.html");
216     navigateTo(m_baseURL + "overflow-scrolling.html");
217
218     // Verify the properties of the accelerated scrolling element starting from the RenderObject
219     // all the way to the WebLayer.
220     Element* scrollableElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("scrollable");
221     ASSERT(scrollableElement);
222
223     RenderObject* renderer = scrollableElement->renderer();
224     ASSERT_TRUE(renderer->isBoxModelObject());
225     ASSERT_TRUE(renderer->hasLayer());
226
227     RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
228     ASSERT_TRUE(layer->usesCompositedScrolling());
229     ASSERT_TRUE(layer->isComposited());
230
231     RenderLayerBacking* layerBacking = layer->backing();
232     ASSERT_TRUE(layerBacking->hasScrollingLayer());
233     ASSERT(layerBacking->scrollingContentsLayer());
234
235     GraphicsLayerChromium* graphicsLayerChromium = static_cast<GraphicsLayerChromium*>(layerBacking->scrollingContentsLayer());
236     ASSERT_EQ(layer, graphicsLayerChromium->scrollableArea());
237
238     WebLayer* webScrollLayer = static_cast<WebLayer*>(layerBacking->scrollingContentsLayer()->platformLayer());
239     ASSERT_TRUE(webScrollLayer->scrollable());
240
241 #if !OS(DARWIN) && !OS(WINDOWS)
242     // Now verify we've attached impl-side scrollbars onto the scrollbar layers
243     ASSERT_TRUE(layerBacking->layerForHorizontalScrollbar());
244     ASSERT_TRUE(layerBacking->layerForHorizontalScrollbar()->hasContentsLayer());
245     ASSERT_TRUE(layerBacking->layerForVerticalScrollbar());
246     ASSERT_TRUE(layerBacking->layerForVerticalScrollbar()->hasContentsLayer());
247 #endif
248 }
249
250 TEST_F(ScrollingCoordinatorChromiumTest, iframeScrolling)
251 {
252     registerMockedHttpURLLoad("iframe-scrolling.html");
253     registerMockedHttpURLLoad("iframe-scrolling-inner.html");
254     navigateTo(m_baseURL + "iframe-scrolling.html");
255
256     // Verify the properties of the accelerated scrolling element starting from the RenderObject
257     // all the way to the WebLayer.
258     Element* scrollableFrame = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("scrollable");
259     ASSERT_TRUE(scrollableFrame);
260
261     RenderObject* renderer = scrollableFrame->renderer();
262     ASSERT_TRUE(renderer);
263     ASSERT_TRUE(renderer->isWidget());
264
265     RenderWidget* renderWidget = toRenderWidget(renderer);
266     ASSERT_TRUE(renderWidget);
267     ASSERT_TRUE(renderWidget->widget());
268     ASSERT_TRUE(renderWidget->widget()->isFrameView());
269
270     FrameView* innerFrameView = static_cast<FrameView*>(renderWidget->widget());
271     RenderView* innerRenderView = innerFrameView->renderView();
272     ASSERT_TRUE(innerRenderView);
273
274     RenderLayerCompositor* innerCompositor = innerRenderView->compositor();
275     ASSERT_TRUE(innerCompositor->inCompositingMode());
276     ASSERT_TRUE(innerCompositor->scrollLayer());
277
278     GraphicsLayerChromium* scrollLayer = static_cast<GraphicsLayerChromium*>(innerCompositor->scrollLayer());
279     ASSERT_EQ(innerFrameView, scrollLayer->scrollableArea());
280
281     WebLayer* webScrollLayer = static_cast<WebLayer*>(scrollLayer->platformLayer());
282     ASSERT_TRUE(webScrollLayer->scrollable());
283
284 #if !OS(DARWIN) && !OS(WINDOWS)
285     // Now verify we've attached impl-side scrollbars onto the scrollbar layers
286     ASSERT_TRUE(innerCompositor->layerForHorizontalScrollbar());
287     ASSERT_TRUE(innerCompositor->layerForHorizontalScrollbar()->hasContentsLayer());
288     ASSERT_TRUE(innerCompositor->layerForVerticalScrollbar());
289     ASSERT_TRUE(innerCompositor->layerForVerticalScrollbar()->hasContentsLayer());
290 #endif
291 }
292
293 } // namespace