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 TapAction(page_action.PageAction): 11 def __init__(self, selector=None, text=None, element_function=None, 12 left_position_percentage=0.5, top_position_percentage=0.5, 13 duration_ms=50, 14 synthetic_gesture_source=page_action.GESTURE_SOURCE_DEFAULT): 15 super(TapAction, self).__init__() 16 self.selector = selector 17 self.text = text 18 self.element_function = element_function 19 self.left_position_percentage = left_position_percentage 20 self.top_position_percentage = top_position_percentage 21 self.duration_ms = duration_ms 22 self._synthetic_gesture_source = ('chrome.gpuBenchmarking.%s_INPUT' % 23 synthetic_gesture_source) 24 25 def WillRunAction(self, tab): 26 utils.InjectJavaScript(tab, 'gesture_common.js') 27 utils.InjectJavaScript(tab, 'tap.js') 28 29 # Fail if browser doesn't support synthetic tap gestures. 30 if not tab.EvaluateJavaScript('window.__TapAction_SupportedByBrowser()'): 31 raise page_action.PageActionNotSupported( 32 'Synthetic tap not supported for this browser') 33 34 tab.ExecuteJavaScript(""" 35 window.__tapActionDone = false; 36 window.__tapAction = new __TapAction(function() { 37 window.__tapActionDone = true; 38 });""") 39 40 def HasElementSelector(self): 41 return (self.element_function is not None or self.selector is not None or 42 self.text is not None) 43 44 def RunAction(self, tab): 45 if not self.HasElementSelector(): 46 self.element_function = 'document.body' 47 48 code = js_template.Render(''' 49 function(element, errorMsg) { 50 if (!element) { 51 throw Error('Cannot find element: ' + errorMsg); 52 } 53 window.__tapAction.start({ 54 element: element, 55 left_position_percentage: {{ left_position_percentage }}, 56 top_position_percentage: {{ top_position_percentage }}, 57 duration_ms: {{ duration_ms }}, 58 gesture_source_type: {{ @gesture_source_type }} 59 }); 60 }''', 61 left_position_percentage=self.left_position_percentage, 62 top_position_percentage=self.top_position_percentage, 63 duration_ms=self.duration_ms, 64 gesture_source_type=self._synthetic_gesture_source) 65 66 page_action.EvaluateCallbackWithElement( 67 tab, code, selector=self.selector, text=self.text, 68 element_function=self.element_function) 69 # The second disjunct handles the case where the tap action leads to an 70 # immediate navigation (in which case the expression below might already be 71 # evaluated on the new page). 72 tab.WaitForJavaScriptCondition( 73 'window.__tapActionDone || window.__tapAction === undefined', 74 timeout=60) 75