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