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()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
63 ASSERT(m_layerTreeView);
64 m_layerTreeView->setRootLayer(rootLayer);
67 virtual WebLayerTreeView* layerTreeView()
69 return m_layerTreeView.get();
73 OwnPtr<WebLayerTreeView> m_layerTreeView;
76 class MockWebFrameClient : public WebFrameClient {
79 class ScrollingCoordinatorChromiumTest : public testing::Test {
81 ScrollingCoordinatorChromiumTest()
82 : m_baseURL("http://www.test.com/")
84 Platform::current()->compositorSupport()->initialize(0);
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()->setFixedPositionCreatesStackingContext(true);
95 m_webViewImpl->initializeMainFrame(&m_mockWebFrameClient);
96 m_webViewImpl->resize(IntSize(320, 240));
99 virtual ~ScrollingCoordinatorChromiumTest()
101 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
102 m_webViewImpl->close();
104 Platform::current()->compositorSupport()->shutdown();
107 void navigateTo(const std::string& url)
109 FrameTestHelpers::loadFrame(m_webViewImpl->mainFrame(), url);
110 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
113 void registerMockedHttpURLLoad(const std::string& fileName)
115 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
118 WebLayer* getRootScrollLayer()
120 RenderLayerCompositor* compositor = m_webViewImpl->mainFrameImpl()->frame()->contentRenderer()->compositor();
122 ASSERT(compositor->scrollLayer());
124 WebLayer* webScrollLayer = static_cast<WebLayer*>(compositor->scrollLayer()->platformLayer());
125 return webScrollLayer;
129 std::string m_baseURL;
130 MockWebFrameClient m_mockWebFrameClient;
131 FakeWebViewClient m_mockWebViewClient;
132 WebViewImpl* m_webViewImpl;
135 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingByDefault)
137 navigateTo("about:blank");
139 // Make sure the scrolling coordinator is active.
140 FrameView* frameView = m_webViewImpl->mainFrameImpl()->frameView();
141 Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
142 ASSERT_TRUE(page->scrollingCoordinator());
143 ASSERT_TRUE(page->scrollingCoordinator()->coordinatesScrollingForFrameView(frameView));
145 // Fast scrolling should be enabled by default.
146 WebLayer* rootScrollLayer = getRootScrollLayer();
147 ASSERT_TRUE(rootScrollLayer->scrollable());
148 ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
149 ASSERT_FALSE(rootScrollLayer->haveWheelEventHandlers());
152 TEST_F(ScrollingCoordinatorChromiumTest, fastScrollingForFixedPosition)
154 registerMockedHttpURLLoad("fixed-position.html");
155 navigateTo(m_baseURL + "fixed-position.html");
157 Page* page = m_webViewImpl->mainFrameImpl()->frame()->page();
158 ASSERT_TRUE(page->scrollingCoordinator()->supportsFixedPositionLayers());
160 // Fixed position should not fall back to main thread scrolling.
161 WebLayer* rootScrollLayer = getRootScrollLayer();
162 ASSERT_FALSE(rootScrollLayer->shouldScrollOnMainThread());
164 // Verify the properties of the fixed position element starting from the RenderObject all the
165 // way to the WebLayer.
166 Element* fixedElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("fixed");
167 ASSERT(fixedElement);
169 RenderObject* renderer = fixedElement->renderer();
170 ASSERT_TRUE(renderer->isBoxModelObject());
171 ASSERT_TRUE(renderer->hasLayer());
173 RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
174 ASSERT_TRUE(layer->isComposited());
176 RenderLayerBacking* layerBacking = layer->backing();
177 WebLayer* webLayer = static_cast<WebLayer*>(layerBacking->graphicsLayer()->platformLayer());
178 ASSERT_TRUE(webLayer->fixedToContainerLayer());
181 TEST_F(ScrollingCoordinatorChromiumTest, nonFastScrollableRegion)
183 registerMockedHttpURLLoad("non-fast-scrollable.html");
184 navigateTo(m_baseURL + "non-fast-scrollable.html");
186 WebLayer* rootScrollLayer = getRootScrollLayer();
187 WebVector<WebRect> nonFastScrollableRegion = rootScrollLayer->nonFastScrollableRegion();
189 ASSERT_EQ(1u, nonFastScrollableRegion.size());
190 ASSERT_EQ(WebRect(8, 8, 10, 10), nonFastScrollableRegion[0]);
193 TEST_F(ScrollingCoordinatorChromiumTest, wheelEventHandler)
195 registerMockedHttpURLLoad("wheel-event-handler.html");
196 navigateTo(m_baseURL + "wheel-event-handler.html");
198 WebLayer* rootScrollLayer = getRootScrollLayer();
199 ASSERT_TRUE(rootScrollLayer->haveWheelEventHandlers());
202 TEST_F(ScrollingCoordinatorChromiumTest, clippedBodyTest)
204 registerMockedHttpURLLoad("clipped-body.html");
205 navigateTo(m_baseURL + "clipped-body.html");
207 WebLayer* rootScrollLayer = getRootScrollLayer();
208 ASSERT_EQ(0u, rootScrollLayer->nonFastScrollableRegion().size());
211 TEST_F(ScrollingCoordinatorChromiumTest, overflowScrolling)
213 registerMockedHttpURLLoad("overflow-scrolling.html");
214 navigateTo(m_baseURL + "overflow-scrolling.html");
216 // Verify the properties of the accelerated scrolling element starting from the RenderObject
217 // all the way to the WebLayer.
218 Element* scrollableElement = m_webViewImpl->mainFrameImpl()->frame()->document()->getElementById("scrollable");
219 ASSERT(scrollableElement);
221 RenderObject* renderer = scrollableElement->renderer();
222 ASSERT_TRUE(renderer->isBoxModelObject());
223 ASSERT_TRUE(renderer->hasLayer());
225 RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
226 ASSERT_TRUE(layer->usesCompositedScrolling());
227 ASSERT_TRUE(layer->isComposited());
229 RenderLayerBacking* layerBacking = layer->backing();
230 ASSERT_TRUE(layerBacking->hasScrollingLayer());
231 ASSERT(layerBacking->scrollingContentsLayer());
233 GraphicsLayerChromium* graphicsLayerChromium = static_cast<GraphicsLayerChromium*>(layerBacking->scrollingContentsLayer());
234 ASSERT_EQ(layer, graphicsLayerChromium->scrollableArea());
236 WebLayer* webScrollLayer = static_cast<WebLayer*>(layerBacking->scrollingContentsLayer()->platformLayer());
237 ASSERT_TRUE(webScrollLayer->scrollable());