• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from telemetry.internal.actions import page_action
6from telemetry.internal.actions import utils
7from telemetry.util import js_template
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    utils.InjectJavaScript(tab, 'gesture_common.js')
32    utils.InjectJavaScript(tab, 'swipe.js')
33
34    # Fail if browser doesn't support synthetic swipe gestures.
35    if not tab.EvaluateJavaScript('window.__SwipeAction_SupportedByBrowser()'):
36      raise page_action.PageActionNotSupported(
37          'Synthetic swipe not supported for this browser')
38
39    if (self._synthetic_gesture_source ==
40        'chrome.gpuBenchmarking.MOUSE_INPUT'):
41      raise page_action.PageActionNotSupported(
42          'Swipe page action does not support mouse input')
43
44    if not page_action.IsGestureSourceTypeSupported(tab, 'touch'):
45      raise page_action.PageActionNotSupported(
46          'Touch input not supported for this browser')
47
48    tab.ExecuteJavaScript("""
49        window.__swipeActionDone = false;
50        window.__swipeAction = new __SwipeAction(function() {
51          window.__swipeActionDone = true;
52        });""")
53
54  def RunAction(self, tab):
55    if (self._selector is None and self._text is None and
56        self._element_function is None):
57      self._element_function = '(document.scrollingElement || document.body)'
58    code = js_template.Render('''
59        function(element, info) {
60          if (!element) {
61            throw Error('Cannot find element: ' + info);
62          }
63          window.__swipeAction.start({
64            element: element,
65            left_start_ratio: {{ left_start_ratio }},
66            top_start_ratio: {{ top_start_ratio }},
67            direction: {{ direction }},
68            distance: {{ distance }},
69            speed: {{ speed }}
70          });
71        }''',
72        left_start_ratio=self._left_start_ratio,
73        top_start_ratio=self._top_start_ratio,
74        direction=self._direction,
75        distance=self._distance,
76        speed=self._speed)
77    page_action.EvaluateCallbackWithElement(
78        tab, code, selector=self._selector, text=self._text,
79        element_function=self._element_function)
80    tab.WaitForJavaScriptCondition('window.__swipeActionDone', timeout=60)
81