• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2012 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.
4import os
5
6from telemetry.page.actions.gesture_action import GestureAction
7from telemetry.page.actions import page_action
8
9class ScrollAction(GestureAction):
10  def __init__(self, attributes=None):
11    super(ScrollAction, self).__init__(attributes)
12
13  def WillRunAction(self, tab):
14    for js_file in ['gesture_common.js', 'scroll.js']:
15      with open(os.path.join(os.path.dirname(__file__), js_file)) as f:
16        js = f.read()
17        tab.ExecuteJavaScript(js)
18
19    # Fail if browser doesn't support synthetic scroll gestures.
20    if not tab.EvaluateJavaScript('window.__ScrollAction_SupportedByBrowser()'):
21      raise page_action.PageActionNotSupported(
22          'Synthetic scroll not supported for this browser')
23
24    # Fail if this action requires touch and we can't send touch events.
25    if hasattr(self, 'scroll_requires_touch'):
26      if (self.scroll_requires_touch and not
27          GestureAction.IsGestureSourceTypeSupported(tab, 'touch')):
28        raise page_action.PageActionNotSupported(
29            'Touch scroll not supported for this browser')
30
31      if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
32          'chrome.gpuBenchmarking.MOUSE_INPUT'):
33        raise page_action.PageActionNotSupported(
34            'Scroll requires touch on this page but mouse input was requested')
35
36    distance_func = 'null'
37    if hasattr(self, 'scroll_distance_function'):
38      distance_func = self.scroll_distance_function
39
40    done_callback = 'function() { window.__scrollActionDone = true; }'
41    tab.ExecuteJavaScript("""
42        window.__scrollActionDone = false;
43        window.__scrollAction = new __ScrollAction(%s, %s);"""
44        % (done_callback, distance_func))
45
46  def RunGesture(self, tab):
47    # scrollable_element_function is a function that passes the scrollable
48    # element on the page to a callback. For example:
49    #   function (callback) {
50    #     callback(document.getElementById('foo'));
51    #   }
52    left_start_percentage = 0.5
53    top_start_percentage = 0.5
54    direction = 'down'
55    speed = 800
56    gesture_source_type = GestureAction.GetGestureSourceTypeFromOptions(tab)
57    if hasattr(self, 'left_start_percentage'):
58      left_start_percentage = self.left_start_percentage
59    if hasattr(self, 'top_start_percentage'):
60      top_start_percentage = self.top_start_percentage
61    if hasattr(self, 'direction'):
62      direction = self.direction
63      if direction not in ['down', 'up', 'left', 'right']:
64        raise page_action.PageActionNotSupported(
65            'Invalid scroll direction: %s' % direction)
66    if hasattr(self, 'speed'):
67      speed = self.speed
68    if hasattr(self, 'scroll_requires_touch') and self.scroll_requires_touch:
69      gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT'
70    if hasattr(self, 'scrollable_element_function'):
71      tab.ExecuteJavaScript("""
72          (%s)(function(element) { window.__scrollAction.start(
73             { element: element,
74               left_start_percentage: %s,
75               top_start_percentage: %s,
76               direction: '%s',
77               speed: %s,
78               gesture_source_type: %s })
79             });""" % (self.scrollable_element_function,
80                       left_start_percentage,
81                       top_start_percentage,
82                       direction,
83                       speed,
84                       gesture_source_type))
85    else:
86      tab.ExecuteJavaScript("""
87          window.__scrollAction.start(
88          { element: document.body,
89            left_start_percentage: %s,
90            top_start_percentage: %s,
91            direction: '%s',
92            speed: %s,
93            gesture_source_type: %s });"""
94        % (left_start_percentage,
95           top_start_percentage,
96           direction,
97           speed,
98           gesture_source_type))
99
100    tab.WaitForJavaScriptExpression('window.__scrollActionDone', 60)
101