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
5from telemetry.internal.actions import page_action
6from telemetry.web_perf import timeline_interaction_record
7
8
9class RepeatableScrollAction(page_action.PageAction):
10
11  def __init__(self, x_scroll_distance_ratio=0.0, y_scroll_distance_ratio=0.5,
12               repeat_count=0, repeat_delay_ms=250, timeout=60):
13    super(RepeatableScrollAction, self).__init__()
14    self._x_scroll_distance_ratio = x_scroll_distance_ratio
15    self._y_scroll_distance_ratio = y_scroll_distance_ratio
16    self._repeat_count = repeat_count
17    self._repeat_delay_ms = repeat_delay_ms
18    self._windowsize = []
19    self._timeout = timeout
20
21  def WillRunAction(self, tab):
22    # Get the dimensions of the screen.
23    window_info_js = 'window.innerWidth + "," + window.innerHeight'
24    js_result = tab.EvaluateJavaScript(window_info_js).split(',')
25
26    self._windowsize = [int(js_result[0]), int(js_result[1])]
27
28  def RunAction(self, tab):
29    # Set up a browser driven repeating scroll. The delay between the scrolls
30    # should be unaffected by render thread responsivness (or lack there of).
31    tab.SynthesizeScrollGesture(
32        x=int(self._windowsize[0] / 2),
33        y=int(self._windowsize[1] / 2),
34        xDistance=int(self._x_scroll_distance_ratio * self._windowsize[0]),
35        yDistance=int(-self._y_scroll_distance_ratio * self._windowsize[1]),
36        repeatCount=self._repeat_count,
37        repeatDelayMs=self._repeat_delay_ms,
38        interactionMarkerName=timeline_interaction_record.GetJavaScriptMarker(
39            'Gesture_ScrollAction', [timeline_interaction_record.REPEATABLE]),
40        timeout=self._timeout)
41