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