| OLD | NEW |
| 1 # Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import os | |
| 6 | |
| 7 from telemetry.internal.actions import scroll | 5 from telemetry.internal.actions import scroll |
| 6 from telemetry.internal.actions import utils |
| 8 from telemetry.testing import tab_test_case | 7 from telemetry.testing import tab_test_case |
| 9 | 8 |
| 10 class ScrollActionTest(tab_test_case.TabTestCase): | 9 class ScrollActionTest(tab_test_case.TabTestCase): |
| 10 def _MakePageVerticallyScrollable(self): |
| 11 # Make page taller than window so it's scrollable vertically. |
| 12 self._tab.ExecuteJavaScript('document.body.style.height =' |
| 13 '(3 * __GestureCommon_GetWindowHeight() + 1) + "px";') |
| 14 |
| 15 def _MakePageHorizontallyScrollable(self): |
| 16 # Make page wider than window so it's scrollable horizontally. |
| 17 self._tab.ExecuteJavaScript('document.body.style.width =' |
| 18 '(3 * __GestureCommon_GetWindowWidth() + 1) + "px";') |
| 19 |
| 20 def setUp(self): |
| 21 tab_test_case.TabTestCase.setUp(self) |
| 22 self.Navigate('blank.html') |
| 23 utils.InjectJavaScript(self._tab, 'gesture_common.js') |
| 24 |
| 11 def testScrollAction(self): | 25 def testScrollAction(self): |
| 12 self.Navigate('blank.html') | |
| 13 | 26 |
| 14 # Make page bigger than window so it's scrollable. | 27 self._MakePageVerticallyScrollable() |
| 15 self._tab.ExecuteJavaScript("""document.body.style.height = | |
| 16 (2 * window.innerHeight + 1) + 'px';""") | |
| 17 | |
| 18 self.assertEquals( | 28 self.assertEquals( |
| 19 self._tab.EvaluateJavaScript("""document.documentElement.scrollTop | 29 self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0) |
| 20 || document.body.scrollTop"""), 0) | |
| 21 | 30 |
| 22 i = scroll.ScrollAction() | 31 i = scroll.ScrollAction() |
| 23 i.WillRunAction(self._tab) | 32 i.WillRunAction(self._tab) |
| 24 | 33 |
| 25 self._tab.ExecuteJavaScript(""" | 34 self._tab.ExecuteJavaScript(""" |
| 26 window.__scrollAction.beginMeasuringHook = function() { | 35 window.__scrollAction.beginMeasuringHook = function() { |
| 27 window.__didBeginMeasuring = true; | 36 window.__didBeginMeasuring = true; |
| 28 }; | 37 }; |
| 29 window.__scrollAction.endMeasuringHook = function() { | 38 window.__scrollAction.endMeasuringHook = function() { |
| 30 window.__didEndMeasuring = true; | 39 window.__didEndMeasuring = true; |
| 31 };""") | 40 };""") |
| 32 i.RunAction(self._tab) | 41 i.RunAction(self._tab) |
| 33 | 42 |
| 34 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring')) | 43 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring')) |
| 35 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring')) | 44 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring')) |
| 36 | 45 |
| 37 scroll_position = self._tab.EvaluateJavaScript( | 46 scroll_position = self._tab.EvaluateJavaScript( |
| 38 '(document.documentElement.scrollTop || document.body.scrollTop)') | 47 'document.scrollingElement.scrollTop') |
| 39 self.assertTrue(scroll_position != 0, | 48 self.assertTrue(scroll_position != 0, |
| 40 msg='scroll_position=%d;' % (scroll_position)) | 49 msg='scroll_position=%d;' % (scroll_position)) |
| 41 | 50 |
| 42 def testDiagonalScrollAction(self): | 51 def testDiagonalScrollAction(self): |
| 43 # Diagonal scrolling was not supported in the ScrollAction until Chrome | 52 # Diagonal scrolling was not supported in the ScrollAction until Chrome |
| 44 # branch number 2332 | 53 # branch number 2332 |
| 45 branch_num = self._tab.browser._browser_backend.devtools_client \ | 54 branch_num = self._tab.browser._browser_backend.devtools_client \ |
| 46 .GetChromeBranchNumber() | 55 .GetChromeBranchNumber() |
| 47 if branch_num < 2332: | 56 if branch_num < 2332: |
| 48 return | 57 return |
| 49 | 58 |
| 50 self.Navigate('blank.html') | 59 self._MakePageVerticallyScrollable() |
| 60 self.assertEquals( |
| 61 self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0) |
| 51 | 62 |
| 52 # Make page bigger than window so it's scrollable. | 63 self._MakePageHorizontallyScrollable() |
| 53 self._tab.ExecuteJavaScript("""document.body.style.height = | |
| 54 (2 * window.innerHeight + 1) + 'px';""") | |
| 55 self._tab.ExecuteJavaScript("""document.body.style.width = | |
| 56 (2 * window.innerWidth + 1) + 'px';""") | |
| 57 | |
| 58 self.assertEquals( | 64 self.assertEquals( |
| 59 self._tab.EvaluateJavaScript("""document.documentElement.scrollTop | 65 self._tab.EvaluateJavaScript('document.scrollingElement.scrollLeft'), 0) |
| 60 || document.body.scrollTop"""), 0) | |
| 61 self.assertEquals( | |
| 62 self._tab.EvaluateJavaScript("""document.documentElement.scrollLeft | |
| 63 || document.body.scrollLeft"""), 0) | |
| 64 | 66 |
| 65 i = scroll.ScrollAction(direction='downright') | 67 i = scroll.ScrollAction(direction='downright') |
| 66 i.WillRunAction(self._tab) | 68 i.WillRunAction(self._tab) |
| 67 | 69 |
| 68 i.RunAction(self._tab) | 70 i.RunAction(self._tab) |
| 69 | 71 |
| 70 viewport_top = self._tab.EvaluateJavaScript( | 72 viewport_top = self._tab.EvaluateJavaScript( |
| 71 '(document.documentElement.scrollTop || document.body.scrollTop)') | 73 'document.scrollingElement.scrollTop') |
| 72 self.assertTrue(viewport_top != 0, msg='viewport_top=%d;' % viewport_top) | 74 self.assertTrue(viewport_top != 0, msg='viewport_top=%d;' % viewport_top) |
| 73 | 75 |
| 74 viewport_left = self._tab.EvaluateJavaScript( | 76 viewport_left = self._tab.EvaluateJavaScript( |
| 75 '(document.documentElement.scrollLeft || document.body.scrollLeft)') | 77 'document.scrollingElement.scrollLeft') |
| 76 self.assertTrue(viewport_left != 0, msg='viewport_left=%d;' % viewport_left) | 78 self.assertTrue(viewport_left != 0, msg='viewport_left=%d;' % viewport_left) |
| 77 | 79 |
| 78 def testBoundingClientRect(self): | 80 def testBoundingClientRect(self): |
| 79 self.Navigate('blank.html') | |
| 80 | |
| 81 with open(os.path.join(os.path.dirname(__file__), | |
| 82 'gesture_common.js')) as f: | |
| 83 js = f.read() | |
| 84 self._tab.ExecuteJavaScript(js) | |
| 85 | |
| 86 # Verify that the rect returned by getBoundingVisibleRect() in scroll.js is | 81 # Verify that the rect returned by getBoundingVisibleRect() in scroll.js is |
| 87 # completely contained within the viewport. Scroll events dispatched by the | 82 # completely contained within the viewport. Scroll events dispatched by the |
| 88 # scrolling API use the center of this rect as their location, and this | 83 # scrolling API use the center of this rect as their location, and this |
| 89 # location needs to be within the viewport bounds to correctly decide | 84 # location needs to be within the viewport bounds to correctly decide |
| 90 # between main-thread and impl-thread scroll. If the scrollable area were | 85 # between main-thread and impl-thread scroll. If the scrollable area were |
| 91 # not clipped to the viewport bounds, then the instance used here (the | 86 # not clipped to the viewport bounds, then the instance used here (the |
| 92 # scrollable area being more than twice as tall as the viewport) would | 87 # scrollable area being more than twice as tall as the viewport) would |
| 93 # result in a scroll location outside of the viewport bounds. | 88 # result in a scroll location outside of the viewport bounds. |
| 94 self._tab.ExecuteJavaScript("""document.body.style.height = | 89 self._MakePageVerticallyScrollable() |
| 95 (3 * window.innerHeight + 1) + 'px';""") | 90 self.assertEquals( |
| 96 self._tab.ExecuteJavaScript("""document.body.style.width = | 91 self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0) |
| 97 (3 * window.innerWidth + 1) + 'px';""") | 92 |
| 98 self._tab.ExecuteJavaScript( | 93 self._MakePageHorizontallyScrollable() |
| 99 "window.scrollTo(window.innerWidth, window.innerHeight);") | 94 self.assertEquals( |
| 95 self._tab.EvaluateJavaScript('document.scrollingElement.scrollLeft'), 0) |
| 96 |
| 97 self._tab.ExecuteJavaScript(""" |
| 98 window.scrollTo(__GestureCommon_GetWindowWidth(), |
| 99 __GestureCommon_GetWindowHeight());""") |
| 100 | 100 |
| 101 rect_top = int(self._tab.EvaluateJavaScript( | 101 rect_top = int(self._tab.EvaluateJavaScript( |
| 102 '__GestureCommon_GetBoundingVisibleRect(document.body).top')) | 102 '__GestureCommon_GetBoundingVisibleRect(document.body).top')) |
| 103 rect_height = int(self._tab.EvaluateJavaScript( | 103 rect_height = int(self._tab.EvaluateJavaScript( |
| 104 '__GestureCommon_GetBoundingVisibleRect(document.body).height')) | 104 '__GestureCommon_GetBoundingVisibleRect(document.body).height')) |
| 105 rect_bottom = rect_top + rect_height | 105 rect_bottom = rect_top + rect_height |
| 106 | 106 |
| 107 rect_left = int(self._tab.EvaluateJavaScript( | 107 rect_left = int(self._tab.EvaluateJavaScript( |
| 108 '__GestureCommon_GetBoundingVisibleRect(document.body).left')) | 108 '__GestureCommon_GetBoundingVisibleRect(document.body).left')) |
| 109 rect_width = int(self._tab.EvaluateJavaScript( | 109 rect_width = int(self._tab.EvaluateJavaScript( |
| 110 '__GestureCommon_GetBoundingVisibleRect(document.body).width')) | 110 '__GestureCommon_GetBoundingVisibleRect(document.body).width')) |
| 111 rect_right = rect_left + rect_width | 111 rect_right = rect_left + rect_width |
| 112 | 112 |
| 113 viewport_height = int(self._tab.EvaluateJavaScript('window.innerHeight')) | 113 viewport_height = int(self._tab.EvaluateJavaScript( |
| 114 viewport_width = int(self._tab.EvaluateJavaScript('window.innerWidth')) | 114 '__GestureCommon_GetWindowHeight()')) |
| 115 viewport_width = int(self._tab.EvaluateJavaScript( |
| 116 '__GestureCommon_GetWindowWidth()')) |
| 115 | 117 |
| 116 self.assertTrue(rect_top >= 0, | 118 self.assertTrue(rect_top >= 0, |
| 117 msg='%s >= %s' % (rect_top, 0)) | 119 msg='%s >= %s' % (rect_top, 0)) |
| 118 self.assertTrue(rect_left >= 0, | 120 self.assertTrue(rect_left >= 0, |
| 119 msg='%s >= %s' % (rect_left, 0)) | 121 msg='%s >= %s' % (rect_left, 0)) |
| 120 self.assertTrue(rect_bottom <= viewport_height, | 122 self.assertTrue(rect_bottom <= viewport_height, |
| 121 msg='%s + %s <= %s' % (rect_top, rect_height, viewport_height)) | 123 msg='%s + %s <= %s' % (rect_top, rect_height, viewport_height)) |
| 122 self.assertTrue(rect_right <= viewport_width, | 124 self.assertTrue(rect_right <= viewport_width, |
| 123 msg='%s + %s <= %s' % (rect_left, rect_width, viewport_width)) | 125 msg='%s + %s <= %s' % (rect_left, rect_width, viewport_width)) |
| OLD | NEW |