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 super(ScrollActionTest, self).setUp()
18 def CreateAndNavigateToPageFromUnittestDataDir(
20 self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
21 page = page_module.Page(
22 self._browser.http_server.UrlOf(filename),
23 None) # In this test, we don't need a page set.
25 self._tab.Navigate(page.url)
26 self._tab.WaitForDocumentReadyStateToBeComplete()
30 @test.Disabled # Disabled due to flakiness: crbug.com/330544
31 def testScrollAction(self):
32 page = self.CreateAndNavigateToPageFromUnittestDataDir("blank.html")
33 setattr(page, 'smoothness', {"action": "scroll"})
34 # Make page bigger than window so it's scrollable.
35 self._tab.ExecuteJavaScript("""document.body.style.height =
36 (2 * window.innerHeight + 1) + 'px';""")
39 self._tab.EvaluateJavaScript("""document.documentElement.scrollTop
40 || document.body.scrollTop"""), 0)
42 i = scroll.ScrollAction()
43 i.WillRunAction(page, self._tab)
45 self._tab.ExecuteJavaScript("""
46 window.__scrollAction.beginMeasuringHook = function() {
47 window.__didBeginMeasuring = true;
49 window.__scrollAction.endMeasuringHook = function() {
50 window.__didEndMeasuring = true;
52 i.RunAction(page, self._tab)
54 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring'))
55 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring'))
57 # Allow for roundoff error in scaled viewport.
58 scroll_position = self._tab.EvaluateJavaScript(
59 """(document.documentElement.scrollTop || document.body.scrollTop)
60 + window.innerHeight""")
61 scroll_height = self._tab.EvaluateJavaScript('document.body.scrollHeight')
62 difference = scroll_position - scroll_height
63 self.assertTrue(abs(difference) <= 1,
64 msg='scroll_position=%d; scroll_height=%d' %
65 (scroll_position, scroll_height))
67 def testBoundingClientRect(self):
68 self.CreateAndNavigateToPageFromUnittestDataDir('blank.html')
69 with open(os.path.join(os.path.dirname(__file__),
70 'gesture_common.js')) as f:
72 self._tab.ExecuteJavaScript(js)
74 # Verify that the rect returned by getBoundingVisibleRect() in scroll.js is
75 # completely contained within the viewport. Scroll events dispatched by the
76 # scrolling API use the center of this rect as their location, and this
77 # location needs to be within the viewport bounds to correctly decide
78 # between main-thread and impl-thread scroll. If the scrollable area were
79 # not clipped to the viewport bounds, then the instance used here (the
80 # scrollable area being more than twice as tall as the viewport) would
81 # result in a scroll location outside of the viewport bounds.
82 self._tab.ExecuteJavaScript("""document.body.style.height =
83 (3 * window.innerHeight + 1) + 'px';""")
84 self._tab.ExecuteJavaScript("""document.body.style.width =
85 (3 * window.innerWidth + 1) + 'px';""")
86 self._tab.ExecuteJavaScript(
87 "window.scrollTo(window.innerWidth, window.innerHeight);")
89 rect_top = int(self._tab.EvaluateJavaScript(
90 '__GestureCommon_GetBoundingVisibleRect(document.body).top'))
91 rect_height = int(self._tab.EvaluateJavaScript(
92 '__GestureCommon_GetBoundingVisibleRect(document.body).height'))
93 rect_bottom = rect_top + rect_height
95 rect_left = int(self._tab.EvaluateJavaScript(
96 '__GestureCommon_GetBoundingVisibleRect(document.body).left'))
97 rect_width = int(self._tab.EvaluateJavaScript(
98 '__GestureCommon_GetBoundingVisibleRect(document.body).width'))
99 rect_right = rect_left + rect_width
101 viewport_height = int(self._tab.EvaluateJavaScript('window.innerHeight'))
102 viewport_width = int(self._tab.EvaluateJavaScript('window.innerWidth'))
104 self.assertTrue(rect_top >= 0,
105 msg='%s >= %s' % (rect_top, 0))
106 self.assertTrue(rect_left >= 0,
107 msg='%s >= %s' % (rect_left, 0))
108 self.assertTrue(rect_bottom <= viewport_height,
109 msg='%s + %s <= %s' % (rect_top, rect_height, viewport_height))
110 self.assertTrue(rect_right <= viewport_width,
111 msg='%s + %s <= %s' % (rect_left, rect_width, viewport_width))