Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / scroll_unittest.py
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.
4
5 import os
6
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
12
13
14 class ScrollActionTest(tab_test_case.TabTestCase):
15   def setUp(self):
16     self._extra_browser_args.append('--enable-gpu-benchmarking')
17     super(ScrollActionTest, self).setUp()
18
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)
26
27     self._tab.Navigate(page.url)
28     self._tab.WaitForDocumentReadyStateToBeComplete()
29
30     return page
31
32   @test.Disabled  # Disabled due to flakiness: crbug.com/330544
33   def testScrollAction(self):
34     page = self.CreateAndNavigateToPageFromUnittestDataDir(
35         "blank.html",
36         page_attributes={"smoothness": {
37           "action": "scroll"
38           }})
39     # Make page bigger than window so it's scrollable.
40     self._tab.ExecuteJavaScript("""document.body.style.height =
41                               (2 * window.innerHeight + 1) + 'px';""")
42
43     self.assertEquals(
44         self._tab.EvaluateJavaScript("""document.documentElement.scrollTop
45                                    || document.body.scrollTop"""), 0)
46
47     i = scroll.ScrollAction()
48     i.WillRunAction(page, self._tab)
49
50     self._tab.ExecuteJavaScript("""
51         window.__scrollAction.beginMeasuringHook = function() {
52             window.__didBeginMeasuring = true;
53         };
54         window.__scrollAction.endMeasuringHook = function() {
55             window.__didEndMeasuring = true;
56         };""")
57     i.RunAction(page, self._tab, None)
58
59     self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring'))
60     self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring'))
61
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))
71
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:
76       js = f.read()
77       self._tab.ExecuteJavaScript(js)
78
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';""")
89
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
95
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
101
102     viewport_height = int(self._tab.EvaluateJavaScript('window.innerHeight'))
103     viewport_width = int(self._tab.EvaluateJavaScript('window.innerWidth'))
104
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))