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.
5 from telemetry
.internal
.actions
import page_action
6 from telemetry
.internal
.actions
import utils
7 from telemetry
.util
import js_template
10 class 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
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
)
30 def WillRunAction(self
, tab
):
31 utils
.InjectJavaScript(tab
, 'gesture_common.js')
32 utils
.InjectJavaScript(tab
, 'swipe.js')
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')
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')
44 if not page_action
.IsGestureSourceTypeSupported(tab
, 'touch'):
45 raise page_action
.PageActionNotSupported(
46 'Touch input not supported for this browser')
48 tab
.ExecuteJavaScript("""
49 window.__swipeActionDone = false;
50 window.__swipeAction = new __SwipeAction(function() {
51 window.__swipeActionDone = true;
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) {
61 throw Error('Cannot find element: ' + info);
63 window.__swipeAction.start({
65 left_start_ratio: {{ left_start_ratio }},
66 top_start_ratio: {{ top_start_ratio }},
67 direction: {{ direction }},
68 distance: {{ distance }},
72 left_start_ratio
=self
._left
_start
_ratio
,
73 top_start_ratio
=self
._top
_start
_ratio
,
74 direction
=self
._direction
,
75 distance
=self
._distance
,
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)