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 5import numbers 6 7from telemetry.internal.actions import page_action 8from telemetry.internal.actions import utils 9from telemetry.web_perf import timeline_interaction_record 10 11 12class RepeatableScrollAction(page_action.PageAction): 13 14 def __init__(self, x_scroll_distance_ratio=0.0, y_scroll_distance_ratio=0.5, 15 repeat_count=0, repeat_delay_ms=250, timeout=60, 16 prevent_fling=None, speed=None): 17 super(RepeatableScrollAction, self).__init__() 18 self._x_scroll_distance_ratio = x_scroll_distance_ratio 19 self._y_scroll_distance_ratio = y_scroll_distance_ratio 20 self._repeat_count = repeat_count 21 self._repeat_delay_ms = repeat_delay_ms 22 self._windowsize = [] 23 self._timeout = timeout 24 self._prevent_fling = prevent_fling 25 self._speed = speed 26 27 def WillRunAction(self, tab): 28 utils.InjectJavaScript(tab, 'gesture_common.js') 29 # Get the dimensions of the screen. 30 self._windowsize = tab.EvaluateJavaScript( 31 '[__GestureCommon_GetWindowWidth(),' 32 ' __GestureCommon_GetWindowHeight()]') 33 assert len(self._windowsize) == 2 34 assert all(isinstance(d, numbers.Number) for d in self._windowsize) 35 36 def RunAction(self, tab): 37 # Set up a browser driven repeating scroll. The delay between the scrolls 38 # should be unaffected by render thread responsivness (or lack there of). 39 tab.SynthesizeScrollGesture( 40 x=int(self._windowsize[0] / 2), 41 y=int(self._windowsize[1] / 2), 42 xDistance=int(self._x_scroll_distance_ratio * self._windowsize[0]), 43 yDistance=int(-self._y_scroll_distance_ratio * self._windowsize[1]), 44 preventFling=self._prevent_fling, 45 speed=self._speed, 46 repeatCount=self._repeat_count, 47 repeatDelayMs=self._repeat_delay_ms, 48 interactionMarkerName=timeline_interaction_record.GetJavaScriptMarker( 49 'Gesture_ScrollAction', [timeline_interaction_record.REPEATABLE]), 50 timeout=self._timeout) 51