2 * Copyright (C) 2012 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.
27 #include "ScrollingCoordinator.h"
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>
48 using namespace WebCore;
49 using namespace WebKit;
53 class FakeWebViewClient : public WebViewClient {
55 virtual WebCompositorOutputSurface* createOutputSurface() OVERRIDE
57 return Platform::current()->compositorSupport()->createOutputSurfaceFor3D(CompositorFakeWebGraphicsContext3D::create(WebGraphicsContext3D::Attributes()).leakPtr());
60 virtual void initializeLayerTreeView(WebLayerTreeViewClient* client, const WebLayer& rootLayer, const WebLayerTreeView::Settings& settings)
62 m_layerTreeView = adoptPtr(Platform::current()->compositorSupport()->createLayerTreeView(client, rootLayer, settings));
63 ASSERT(m_layerTreeView);
66 virtual WebLayerTreeView* layerTreeView()
68 return m_layerTreeView.get();
72 OwnPtr<WebLayerTreeView> m_layerTreeView;
75 class MockWebFrameClient : public WebFrameClient {
78 class ScrollingCoordinatorChromiumTest : public testing::Test {
80 ScrollingCoordinatorChromiumTest()
81 : m_baseURL("http://www.test.com/")
83 Platform::current()->compositorSupport()->initialize(0);
85 // We cannot reuse FrameTestHelpers::createWebViewAndLoad here because the compositing
86 // settings need to be set before the page is loaded.
87 m_webViewImpl = static_cast<WebViewImpl*>(WebView::create(&m_mockWebViewClient));
88 m_webViewImpl->settings()->setJavaScriptEnabled(true);
89 m_webViewImpl->settings()->setForceCompositingMode(true);
90 m_webViewImpl->settings()->setAcceleratedCompositingEnabled(true);
91 m_webViewImpl->settings()->setAcceleratedCompositingForFixedPositionEnabled(true);
92 m_webViewImpl->settings()->setAcceleratedCompositingForOverflowScrollEnabled(true);
93 m_webViewImpl->settings()->setFixedPositionCreatesStackingContext(true);
94 m_webViewImpl->initializeMainFrame(&m_mockWebFrameClient);
95 m_webViewImpl->resize(IntSize(320, 240));
98 virtual ~ScrollingCoordinatorChromiumTest()
100 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
101 m_webViewImpl->close();
103 Platform::current()->compositorSupport()->shutdown();
106 void navigateTo(const std::string& url)
108 FrameTestHelpers::loadFrame(m_webViewImpl->mainFrame(), url);
109 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
112 void registerMockedHttpURLLoad(const std::string& fileName)
114 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
117 WebLayer* getRootScrollLayer()
119 RenderLayerCompositor* compositor = m_webViewImpl->mainFrameImpl()->frame()->contentRenderer()->compositor();
121 ASSERT(compositor->scrollLayer());
123 WebLayer* webScrollLayer = static_cast<WebLayer*>(compositor->scrollLayer()->platformLayer());
124 return webScrollLayer;
128 std::string m_baseURL;
129 MockWebFrameClient m_mockWebFrameClient;
130 FakeWebViewClient m_mockWebViewClient;
131 WebViewImpl* m_webViewImpl;
134 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingByDefault)
136 navigateTo("about:blank");
138 // Make sure the scrolling coordinator is active.
139 FrameView* frameView = m_webViewImpl->mainFrameImpl()->frameView();
140 Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
141 ASSERT_TRUE(page->scrollingCoordinator());
142 ASSERT_TRUE(page->scrollingCoordinator()->coordinatesScrollingForFrameView(frameView));
144 // Fast scrolling should be enabled by default.
145 WebLayer* rootScrollLayer = getRootScrollLayer();
146 ASSERT_TRUE(rootScrollLayer->scrollable());
147 ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
148 ASSERT_FALSE(rootScrollLayer->haveWheelEventHandlers());
151 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingForFixedPosition)
153 registerMockedHttpURLLoad("fixed-position.html");
154 navigateTo(m_baseURL + "fixed-position.html");
156 Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
157 ASSERT_TRUE(page->scrollingCoordinator()->supportsFixedPositionLayers());
159 // Fixed position should not fall back to main thread scrolling.
160 WebLayer* rootScrollLayer = getRootScrollLayer();
161 ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
163 // Verify the properties of the fixed position element starting from the RenderObject all the
164 // way to the WebLayer.
165 Element* fixedElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("fixed");
166 ASSERT(fixedElement);
168 RenderObject* renderer = fixedElement->renderer();
169 ASSERT_TRUE(renderer->isBoxModelObject());
170 ASSERT_TRUE(renderer->hasLayer());
172 RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
173 ASSERT_TRUE(layer->isComposited());
175 RenderLayerBacking* layerBacking = layer->backing();
176 WebLayer* webLayer = static_cast<WebLayer*>(layerBacking->graphicsLayer()->platformLayer());
177 ASSERT_TRUE(webLayer->fixedToContainerLayer());
180 TEST_F(ScrollingCoordinatorChromiumTest, nonFastScrollableRegion)
182 registerMockedHttpURLLoad("non-fast-scrollable.html");
183 navigateTo(m_baseURL + "non-fast-scrollable.html");
185 WebLayer* rootScrollLayer = getRootScrollLayer();
186 WebVector<WebRect> nonFastScrollableRegion = rootScrollLayer->nonFastScrollableRegion();
188 ASSERT_EQ(1u, nonFastScrollableRegion.size());
189 ASSERT_EQ(WebRect(8, 8, 10, 10), nonFastScrollableRegion[0]);
192 TEST_F(ScrollingCoordinatorChromiumTest, wheelEventHandler)
194 registerMockedHttpURLLoad("wheel-event-handler.html");
195 navigateTo(m_baseURL + "wheel-event-handler.html");
197 WebLayer* rootScrollLayer = getRootScrollLayer();
198 ASSERT_TRUE(rootScrollLayer->haveWheelEventHandlers());
201 TEST_F(ScrollingCoordinatorChromiumTest, clippedBodyTest)
203 registerMockedHttpURLLoad("clipped-body.html");
204 navigateTo(m_baseURL + "clipped-body.html");
206 WebLayer* rootScrollLayer = getRootScrollLayer();
207 ASSERT_EQ(0u, rootScrollLayer->nonFastScrollableRegion().size());
210 TEST_F(ScrollingCoordinatorChromiumTest, overflowScrolling)
212 registerMockedHttpURLLoad("overflow-scrolling.html");
213 navigateTo(m_baseURL + "overflow-scrolling.html");
215 // Verify the properties of the accelerated scrolling element starting from the RenderObject
216 // all the way to the WebLayer.
217 Element* scrollableElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("scrollable");
218 ASSERT(scrollableElement);
220 RenderObject* renderer = scrollableElement->renderer();
221 ASSERT_TRUE(renderer->isBoxModelObject());
222 ASSERT_TRUE(renderer->hasLayer());
224 RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
225 ASSERT_TRUE(layer->usesCompositedScrolling());
226 ASSERT_TRUE(layer->isComposited());
228 RenderLayerBacking* layerBacking = layer->backing();
229 ASSERT_TRUE(layerBacking->hasScrollingLayer());
230 ASSERT(layerBacking->scrollingContentsLayer());
232 GraphicsLayerChromium* graphicsLayerChromium = static_cast<GraphicsLayerChromium*>(layerBacking->scrollingContentsLayer());
233 ASSERT_EQ(layer, graphicsLayerChromium->scrollableArea());
235 WebLayer* webScrollLayer = static_cast<WebLayer*>(layerBacking->scrollingContentsLayer()->platformLayer());
236 ASSERT_TRUE(webScrollLayer->scrollable());