1# Copyright 2013 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 os
6
7from telemetry.internal.actions import page_action
8
9
10class SwipeAction(page_action.PageAction):
11  def __init__(self, selector=None, text=None, element_function=None,
12               left_start_ratio=0.5, top_start_ratio=0.5,
13               direction='left', distance=100, speed_in_pixels_per_second=800,
14               synthetic_gesture_source=page_action.GESTURE_SOURCE_DEFAULT):
15    super(SwipeAction, self).__init__()
16    if direction not in ['down', 'up', 'left', 'right']:
17      raise page_action.PageActionNotSupported(
18          'Invalid swipe direction: %s' % self.direction)
19    self._selector = selector
20    self._text = text
21    self._element_function = element_function
22    self._left_start_ratio = left_start_ratio
23    self._top_start_ratio = top_start_ratio
24    self._direction = direction
25    self._distance = distance
26    self._speed = speed_in_pixels_per_second
27    self._synthetic_gesture_source = ('chrome.gpuBenchmarking.%s_INPUT' %
28                                      synthetic_gesture_source)
29
30  def WillRunAction(self, tab):
31    for js_file in ['gesture_common.js', 'swipe.js']:
32      with open(os.path.join(os.path.dirname(__file__), js_file)) as f:
33        js = f.read()
34        tab.ExecuteJavaScript(js)
35
36    # Fail if browser doesn't support synthetic swipe gestures.
37    if not tab.EvaluateJavaScript('window.__SwipeAction_SupportedByBrowser()'):
38      raise page_action.PageActionNotSupported(
39          'Synthetic swipe not supported for this browser')
40
41    if (self._synthetic_gesture_source ==
42        'chrome.gpuBenchmarking.MOUSE_INPUT'):
43      raise page_action.PageActionNotSupported(
44          'Swipe page action does not support mouse input')
45
46    if not page_action.IsGestureSourceTypeSupported(tab, 'touch'):
47      raise page_action.PageActionNotSupported(
48          'Touch input not supported for this browser')
49
50    done_callback = 'function() { window.__swipeActionDone = true; }'
51    tab.ExecuteJavaScript("""
52        window.__swipeActionDone = false;
53        window.__swipeAction = new __SwipeAction(%s);"""
54        % (done_callback))
55
56  def RunAction(self, tab):
57    if (self._selector is None and self._text is None and
58        self._element_function is None):
59      self._element_function = '(document.scrollingElement || document.body)'
60    code = '''
61        function(element, info) {
62          if (!element) {
63            throw Error('Cannot find element: ' + info);
64          }
65          window.__swipeAction.start({
66            element: element,
67            left_start_ratio: %s,
68            top_start_ratio: %s,
69            direction: '%s',
70            distance: %s,
71            speed: %s
72          });
73        }''' % (self._left_start_ratio,
74                self._top_start_ratio,
75                self._direction,
76                self._distance,
77                self._speed)
78    page_action.EvaluateCallbackWithElement(
79        tab, code, selector=self._selector, text=self._text,
80        element_function=self._element_function)
81    tab.WaitForJavaScriptExpression('window.__swipeActionDone', 60)
82