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