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.
6 from telemetry.page.actions import gesture_action
7 from telemetry.page.actions import page_action
9 class TapAction(gesture_action.GestureAction):
10 def __init__(self, attributes=None):
11 super(TapAction, self).__init__(attributes)
12 self._SetTimelineMarkerBaseName('TapAction::RunAction')
14 def WillRunAction(self, page, tab):
15 for js_file in ['gesture_common.js', 'tap.js']:
16 with open(os.path.join(os.path.dirname(__file__), js_file)) as f:
18 tab.ExecuteJavaScript(js)
20 # Fail if browser doesn't support synthetic tap gestures.
21 if not tab.EvaluateJavaScript('window.__TapAction_SupportedByBrowser()'):
22 raise page_action.PageActionNotSupported(
23 'Synthetic tap not supported for this browser')
25 done_callback = 'function() { window.__tapActionDone = true; }'
26 tab.ExecuteJavaScript("""
27 window.__tapActionDone = false;
28 window.__tapAction = new __TapAction(%s);"""
31 def RunGesture(self, page, tab, previous_action):
32 left_position_percentage = 0.5
33 top_position_percentage = 0.5
35 if hasattr(self, 'left_position_percentage'):
36 left_position_percentage = self.left_position_percentage
37 if hasattr(self, 'top_position_percentage'):
38 top_position_percentage = self.top_position_percentage
39 if hasattr(self, 'duration_ms'):
40 duration_ms = self.duration_ms
41 if hasattr(self, 'element_function'):
42 tab.ExecuteJavaScript("""
43 (%s)(function(element) { window.__tapAction.start(
45 left_position_percentage: %s,
46 top_position_percentage: %s,
48 });""" % (self.element_function,
49 left_position_percentage,
50 top_position_percentage,
53 tab.ExecuteJavaScript("""
54 window.__tapAction.start(
55 { element: document.body,
56 left_position_percentage: %s,
57 top_position_percentage: %s,
58 duration_ms: %s });"""
59 % (left_position_percentage,
60 top_position_percentage,
63 tab.WaitForJavaScriptExpression('window.__tapActionDone', 60)
68 def BindMeasurementJavaScript(self, tab, start_js, stop_js):
69 # Make the tap action start and stop measurement automatically.
70 tab.ExecuteJavaScript("""
71 window.__tapAction.beginMeasuringHook = function() { %s };
72 window.__tapAction.endMeasuringHook = function() { %s };
73 """ % (start_js, stop_js))