1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
7 from telemetry.core import util
8 from telemetry.page import page as page_module
9 from telemetry.page.actions import scroll
10 from telemetry.unittest import tab_test_case
11 from telemetry.unittest import test
14 class ScrollActionTest(tab_test_case.TabTestCase):
16 self._extra_browser_args.append('--enable-gpu-benchmarking')
17 super(ScrollActionTest, self).setUp()
19 def CreateAndNavigateToPageFromUnittestDataDir(
20 self, filename, page_attributes):
21 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
22 page = page_module.Page(
23 self._browser.http_server.UrlOf(filename),
24 None, # In this test, we don't need a page set.
25 attributes=page_attributes)
27 self._tab.Navigate(page.url)
28 self._tab.WaitForDocumentReadyStateToBeComplete()
32 @test.Disabled # Disabled due to flakiness: crbug.com/330544
33 def testScrollAction(self):
34 page = self.CreateAndNavigateToPageFromUnittestDataDir(
36 page_attributes={"smoothness": {
39 # Make page bigger than window so it's scrollable.
40 self._tab.ExecuteJavaScript("""document.body.style.height =
41 (2 * window.innerHeight + 1) + 'px';""")
44 self._tab.EvaluateJavaScript("""document.documentElement.scrollTop
45 || document.body.scrollTop"""), 0)
47 i = scroll.ScrollAction()
48 i.WillRunAction(page, self._tab)
50 self._tab.ExecuteJavaScript("""
51 window.__scrollAction.beginMeasuringHook = function() {
52 window.__didBeginMeasuring = true;
54 window.__scrollAction.endMeasuringHook = function() {
55 window.__didEndMeasuring = true;
57 i.RunAction(page, self._tab, None)
59 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring'))
60 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring'))
62 # Allow for roundoff error in scaled viewport.
63 scroll_position = self._tab.EvaluateJavaScript(
64 """(document.documentElement.scrollTop || document.body.scrollTop)
65 + window.innerHeight""")
66 scroll_height = self._tab.EvaluateJavaScript('document.body.scrollHeight')
67 difference = scroll_position - scroll_height
68 self.assertTrue(abs(difference) <= 1,
69 msg='scroll_position=%d; scroll_height=%d' %
70 (scroll_position, scroll_height))
72 def testBoundingClientRect(self):
73 self.CreateAndNavigateToPageFromUnittestDataDir('blank.html', {})
74 with open(os.path.join(os.path.dirname(__file__),
75 'gesture_common.js')) as f:
77 self._tab.ExecuteJavaScript(js)
79 # Verify that the rect returned by getBoundingVisibleRect() in scroll.js is
80 # completely contained within the viewport. Scroll events dispatched by the
81 # scrolling API use the center of this rect as their location, and this
82 # location needs to be within the viewport bounds to correctly decide
83 # between main-thread and impl-thread scroll. If the scrollable area were
84 # not clipped to the viewport bounds, then the instance used here (the
85 # scrollable area being more than twice as tall as the viewport) would
86 # result in a scroll location outside of the viewport bounds.
87 self._tab.ExecuteJavaScript("""document.body.style.height =
88 (2 * window.innerHeight + 1) + 'px';""")
90 rect_top = int(self._tab.EvaluateJavaScript(
91 '__GestureCommon_GetBoundingVisibleRect(document.body).top'))
92 rect_height = int(self._tab.EvaluateJavaScript(
93 '__GestureCommon_GetBoundingVisibleRect(document.body).height'))
94 rect_bottom = rect_top + rect_height
96 rect_left = int(self._tab.EvaluateJavaScript(
97 '__GestureCommon_GetBoundingVisibleRect(document.body).left'))
98 rect_width = int(self._tab.EvaluateJavaScript(
99 '__GestureCommon_GetBoundingVisibleRect(document.body).width'))
100 rect_right = rect_left + rect_width
102 viewport_height = int(self._tab.EvaluateJavaScript('window.innerHeight'))
103 viewport_width = int(self._tab.EvaluateJavaScript('window.innerWidth'))
105 self.assertTrue(rect_bottom <= viewport_height,
106 msg='%s + %s <= %s' % (rect_top, rect_height, viewport_height))
107 self.assertTrue(rect_right <= viewport_width,
108 msg='%s + %s <= %s' % (rect_left, rect_width, viewport_width))