1# Copyright 2016 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 5from telemetry import decorators 6from telemetry.internal.actions import scroll_to_element 7from telemetry.internal.actions import utils 8from telemetry.testing import tab_test_case 9 10 11class ScrollToElementActionTest(tab_test_case.TabTestCase): 12 13 def _MakePageVerticallyScrollable(self): 14 # Make page taller than window so it's scrollable vertically. 15 self._tab.ExecuteJavaScript('document.body.style.height =' 16 '(6 * __GestureCommon_GetWindowHeight() + 1) + "px";') 17 18 def _VisibleAreaOfElement(self, selector='#element'): 19 return self._tab.EvaluateJavaScript(""" 20 (function() { 21 var element = document.querySelector({{ selector }}); 22 var rect = __GestureCommon_GetBoundingVisibleRect(element); 23 return rect.width * rect.height; 24 })() 25 """, selector=selector) 26 27 def _InsertContainer(self, theid='container'): 28 self._tab.ExecuteJavaScript(""" 29 var container = document.createElement("div") 30 container.id = {{ theid }}; 31 container.style.position = 'relative'; 32 container.style.height = '100%'; 33 document.body.appendChild(container); 34 """, theid=theid) 35 36 def _InsertElement(self, theid='element', container_selector='body'): 37 self._tab.ExecuteJavaScript(""" 38 var container = document.querySelector({{ container_selector }}); 39 var element = document.createElement("div"); 40 element.id = {{ theid }}; 41 element.textContent = 'My element'; 42 element.style.position = 'absolute'; 43 element.style.top = (__GestureCommon_GetWindowHeight() * 3) + "px"; 44 container.appendChild(element); 45 """, theid=theid, container_selector=container_selector) 46 47 def setUp(self): 48 tab_test_case.TabTestCase.setUp(self) 49 self.Navigate('blank.html') 50 utils.InjectJavaScript(self._tab, 'gesture_common.js') 51 52 # https://github.com/catapult-project/catapult/issues/3099 53 @decorators.Disabled('android') 54 def testScrollToElement(self): 55 self._MakePageVerticallyScrollable() 56 self._InsertElement() 57 self.assertEquals( 58 self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0) 59 60 # Before we scroll down the element should not be visible at all. 61 self.assertEquals(self._VisibleAreaOfElement(), 0) 62 63 i = scroll_to_element.ScrollToElementAction(selector='#element') 64 i.WillRunAction(self._tab) 65 i.RunAction(self._tab) 66 67 # After we scroll down at least some of the element should be visible. 68 self.assertGreater(self._VisibleAreaOfElement(selector='#element'), 0) 69 70 # https://github.com/catapult-project/catapult/issues/3099 71 @decorators.Disabled('android') 72 def testScrollContainerToElement(self): 73 self._MakePageVerticallyScrollable() 74 self._InsertContainer() 75 self._InsertElement(container_selector='#container') 76 self.assertEquals( 77 self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0) 78 79 # Before we scroll down the element should not be visible at all. 80 self.assertEquals(self._VisibleAreaOfElement(), 0) 81 82 i = scroll_to_element.ScrollToElementAction( 83 selector='#element', container_selector='#container') 84 i.WillRunAction(self._tab) 85 i.RunAction(self._tab) 86 87 # After we scroll down at least some of the element should be visible. 88 self.assertGreater(self._VisibleAreaOfElement(), 0) 89