Home | History | Annotate | Download | only in actions
      1 # Copyright 2015 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 from telemetry import decorators
      6 
      7 from telemetry.internal.actions import page_action
      8 from telemetry.internal.actions import repeatable_scroll
      9 from telemetry.internal.actions import utils
     10 from telemetry.internal.browser import browser_info as browser_info_module
     11 from telemetry.testing import tab_test_case
     12 
     13 
     14 class RepeatableScrollActionTest(tab_test_case.TabTestCase):
     15 
     16   def setUp(self):
     17     tab_test_case.TabTestCase.setUp(self)
     18     self.Navigate('blank.html')
     19     utils.InjectJavaScript(self._tab, 'gesture_common.js')
     20 
     21     # Make page taller than window so it's scrollable.
     22     self._tab.ExecuteJavaScript('document.body.style.height ='
     23         '(3 * __GestureCommon_GetWindowHeight() + 1) + "px";')
     24 
     25     self.assertEquals(
     26         self._tab.EvaluateJavaScript('document.scrollingElement.scrollTop'), 0)
     27 
     28     self._browser_info = browser_info_module.BrowserInfo(self._tab.browser)
     29     self._window_height = int(self._tab.EvaluateJavaScript(
     30         '__GestureCommon_GetWindowHeight()'))
     31 
     32   def testRepeatableScrollActionNoRepeats(self):
     33     if not self._browser_info.HasRepeatableSynthesizeScrollGesture():
     34       return
     35 
     36     expected_scroll = (self._window_height / 2) - 1
     37 
     38     i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5)
     39     i.WillRunAction(self._tab)
     40 
     41     i.RunAction(self._tab)
     42 
     43     scroll_position = self._tab.EvaluateJavaScript(
     44         'document.scrollingElement.scrollTop')
     45     # We can only expect the final scroll position to be approximatly equal.
     46     self.assertTrue(abs(scroll_position - expected_scroll) < 20,
     47                     msg='scroll_position=%d;expected %d' % (scroll_position,
     48                                                             expected_scroll))
     49 
     50   def testRepeatableScrollActionTwoRepeats(self):
     51     if not self._browser_info.HasRepeatableSynthesizeScrollGesture():
     52       return
     53 
     54     expected_scroll = ((self._window_height / 2) - 1) * 3
     55 
     56     i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5,
     57                                                  repeat_count=2,
     58                                                  repeat_delay_ms=1)
     59     i.WillRunAction(self._tab)
     60 
     61     i.RunAction(self._tab)
     62 
     63     scroll_position = self._tab.EvaluateJavaScript(
     64         'document.scrollingElement.scrollTop')
     65     # We can only expect the final scroll position to be approximatly equal.
     66     self.assertTrue(abs(scroll_position - expected_scroll) < 20,
     67                     msg='scroll_position=%d;expected %d' % (scroll_position,
     68                                                             expected_scroll))
     69 
     70   # Regression test for crbug.com/627166
     71   # TODO(ulan): enable for Android after catapult:#2475 is fixed.
     72   @decorators.Disabled('all')
     73   def testRepeatableScrollActionNoRepeatsZoomed(self):
     74     if (not self._browser_info.HasRepeatableSynthesizeScrollGesture() or
     75         not page_action.IsGestureSourceTypeSupported(self._tab, 'touch')):
     76       return
     77 
     78     self._tab.action_runner.PinchPage(scale_factor=0.1)
     79 
     80     inner_height = self._tab.EvaluateJavaScript('window.innerHeight')
     81     outer_height = self._tab.EvaluateJavaScript('window.outerHeight')
     82 
     83     self.assertGreater(inner_height, outer_height)
     84 
     85     i = repeatable_scroll.RepeatableScrollAction(y_scroll_distance_ratio=0.5)
     86     i.WillRunAction(self._tab)
     87     i.RunAction(self._tab)
     88     # If scroll coordinates are computed incorrectly Chrome will crash with
     89     # [FATAL:synthetic_gesture_target_base.cc(62)] Check failed:
     90     # web_touch.touches[i].state != WebTouchPoint::StatePressed ||
     91     # PointIsWithinContents(web_touch.touches[i].position.x,
     92     # web_touch.touches[i].position.y). Touch coordinates are not within content
     93     # bounds on TouchStart.
     94