swipe.py revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4import os
5
6from telemetry.page.actions.gesture_action import GestureAction
7from telemetry.page.actions import page_action
8
9class SwipeAction(GestureAction):
10  def __init__(self, attributes=None):
11    super(SwipeAction, self).__init__(attributes)
12
13  def WillRunAction(self, tab):
14    for js_file in ['gesture_common.js', 'swipe.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 swipe gestures.
20    if not tab.EvaluateJavaScript('window.__SwipeAction_SupportedByBrowser()'):
21      raise page_action.PageActionNotSupported(
22          'Synthetic swipe not supported for this browser')
23
24    if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
25        'chrome.gpuBenchmarking.MOUSE_INPUT'):
26      raise page_action.PageActionNotSupported(
27          'Swipe page action does not support mouse input')
28
29    if not GestureAction.IsGestureSourceTypeSupported(tab, 'touch'):
30      raise page_action.PageActionNotSupported(
31          'Touch input not supported for this browser')
32
33    done_callback = 'function() { window.__swipeActionDone = true; }'
34    tab.ExecuteJavaScript("""
35        window.__swipeActionDone = false;
36        window.__swipeAction = new __SwipeAction(%s);"""
37        % (done_callback))
38
39  def RunGesture(self, tab):
40    left_start_percentage = 0.5
41    top_start_percentage = 0.5
42    direction = 'left'
43    distance = 100
44    speed = 800
45    if hasattr(self, 'left_start_percentage'):
46      left_start_percentage = self.left_start_percentage
47    if hasattr(self, 'top_start_percentage'):
48      top_start_percentage = self.top_start_percentage
49    if hasattr(self, 'direction'):
50      direction = self.direction
51      if direction not in ['down', 'up', 'left', 'right']:
52        raise page_action.PageActionNotSupported(
53            'Invalid swipe direction: %s' % direction)
54    if hasattr(self, 'distance'):
55      distance = self.distance
56    if hasattr(self, 'speed'):
57      speed = self.speed
58    if hasattr(self, 'element_function'):
59      tab.ExecuteJavaScript("""
60          (%s)(function(element) { window.__swipeAction.start(
61             { element: element,
62               left_start_percentage: %s,
63               top_start_percentage: %s,
64               direction: '%s',
65               distance: %s,
66               speed: %s })
67             });""" % (self.element_function,
68                       left_start_percentage,
69                       top_start_percentage,
70                       direction,
71                       distance,
72                       speed))
73    else:
74      tab.ExecuteJavaScript("""
75          window.__swipeAction.start(
76          { element: document.body,
77            left_start_percentage: %s,
78            top_start_percentage: %s,
79            direction: '%s',
80            distance: %s,
81            speed: %s });"""
82        % (left_start_percentage,
83           top_start_percentage,
84           direction,
85           distance,
86           speed))
87
88    tab.WaitForJavaScriptExpression('window.__swipeActionDone', 60)
89
90  def CanBeBound(self):
91    return True
92
93  def BindMeasurementJavaScript(self, tab, start_js, stop_js):
94    # Make the swipe action start and stop measurement automatically.
95    tab.ExecuteJavaScript("""
96        window.__swipeAction.beginMeasuringHook = function() { %s };
97        window.__swipeAction.endMeasuringHook = function() { %s };
98    """ % (start_js, stop_js))
99