| Index: telemetry/telemetry/internal/actions/scroll_to_element_unittest.py
|
| diff --git a/telemetry/telemetry/internal/actions/scroll_to_element_unittest.py b/telemetry/telemetry/internal/actions/scroll_to_element_unittest.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bb31541a6a88e514744961bd1e79b92102a662d3
|
| --- /dev/null
|
| +++ b/telemetry/telemetry/internal/actions/scroll_to_element_unittest.py
|
| @@ -0,0 +1,99 @@
|
| +# Copyright 2016 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +from telemetry import decorators
|
| +from telemetry.internal.actions import scroll_to_element
|
| +from telemetry.internal.actions import utils
|
| +from telemetry.testing import tab_test_case
|
| +from telemetry.util import js_template
|
| +
|
| +
|
| +class ScrollToElementActionTest(tab_test_case.TabTestCase):
|
| +
|
| + def _ExecuteJavaScript(self, *args, **kwargs):
|
| + # TODO(catapult:#3028): Render in JavaScript method when supported by API.
|
| + code = js_template.Render(*args, **kwargs)
|
| + self._tab.ExecuteJavaScript(code)
|
| +
|
| + def _EvaluateJavaScript(self, *args, **kwargs):
|
| + # TODO(catapult:#3028): Render in JavaScript method when supported by API.
|
| + code = js_template.Render(*args, **kwargs)
|
| + return self._tab.EvaluateJavaScript(code)
|
| +
|
| + def _MakePageVerticallyScrollable(self):
|
| + # Make page taller than window so it's scrollable vertically.
|
| + self._ExecuteJavaScript('document.body.style.height ='
|
| + '(6 * __GestureCommon_GetWindowHeight() + 1) + "px";')
|
| +
|
| + def _VisibleAreaOfElement(self, selector='#element'):
|
| + return self._EvaluateJavaScript("""
|
| + (function() {
|
| + var element = document.querySelector({{ selector }});
|
| + var rect = __GestureCommon_GetBoundingVisibleRect(element);
|
| + return rect.width * rect.height;
|
| + })()
|
| + """, selector=selector)
|
| +
|
| + def _InsertContainer(self, theid='container'):
|
| + self._ExecuteJavaScript("""
|
| + var container = document.createElement("div")
|
| + container.id = {{ theid }};
|
| + container.style.position = 'relative';
|
| + container.style.height = '100%';
|
| + document.body.appendChild(container);
|
| + """, theid=theid)
|
| +
|
| + def _InsertElement(self, theid='element', container_selector='body'):
|
| + self._ExecuteJavaScript("""
|
| + var container = document.querySelector({{ container_selector }});
|
| + var element = document.createElement("div");
|
| + element.id = {{ theid }};
|
| + element.textContent = 'My element';
|
| + element.style.position = 'absolute';
|
| + element.style.top = (__GestureCommon_GetWindowHeight() * 3) + "px";
|
| + container.appendChild(element);
|
| + """, theid=theid, container_selector=container_selector)
|
| +
|
| + def setUp(self):
|
| + tab_test_case.TabTestCase.setUp(self)
|
| + self.Navigate('blank.html')
|
| + utils.InjectJavaScript(self._tab, 'gesture_common.js')
|
| +
|
| + # https://github.com/catapult-project/catapult/issues/3099
|
| + @decorators.Disabled('android')
|
| + def testScrollToElement(self):
|
| + self._MakePageVerticallyScrollable()
|
| + self._InsertElement()
|
| + self.assertEquals(
|
| + self._EvaluateJavaScript('document.scrollingElement.scrollTop'), 0)
|
| +
|
| + # Before we scroll down the element should not be visible at all.
|
| + self.assertEquals(self._VisibleAreaOfElement(), 0)
|
| +
|
| + i = scroll_to_element.ScrollToElementAction(selector='#element')
|
| + i.WillRunAction(self._tab)
|
| + i.RunAction(self._tab)
|
| +
|
| + # After we scroll down at least some of the element should be visible.
|
| + self.assertGreater(self._VisibleAreaOfElement(selector='#element'), 0)
|
| +
|
| + # https://github.com/catapult-project/catapult/issues/3099
|
| + @decorators.Disabled('android')
|
| + def testScrollContainerToElement(self):
|
| + self._MakePageVerticallyScrollable()
|
| + self._InsertContainer()
|
| + self._InsertElement(container_selector='#container')
|
| + self.assertEquals(
|
| + self._EvaluateJavaScript('document.scrollingElement.scrollTop'), 0)
|
| +
|
| + # Before we scroll down the element should not be visible at all.
|
| + self.assertEquals(self._VisibleAreaOfElement(), 0)
|
| +
|
| + i = scroll_to_element.ScrollToElementAction(
|
| + selector='#element', container_selector='#container')
|
| + i.WillRunAction(self._tab)
|
| + i.RunAction(self._tab)
|
| +
|
| + # After we scroll down at least some of the element should be visible.
|
| + self.assertGreater(self._VisibleAreaOfElement(), 0)
|
|
|