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 5import os 6 7from telemetry.internal.actions import page_action 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 for js_file in ['gesture_common.js', 'tap.js']: 27 with open(os.path.join(os.path.dirname(__file__), js_file)) as f: 28 js = f.read() 29 tab.ExecuteJavaScript(js) 30 31 # Fail if browser doesn't support synthetic tap gestures. 32 if not tab.EvaluateJavaScript('window.__TapAction_SupportedByBrowser()'): 33 raise page_action.PageActionNotSupported( 34 'Synthetic tap not supported for this browser') 35 36 done_callback = 'function() { window.__tapActionDone = true; }' 37 tab.ExecuteJavaScript(""" 38 window.__tapActionDone = false; 39 window.__tapAction = new __TapAction(%s);""" 40 % (done_callback)) 41 42 def HasElementSelector(self): 43 return (self.element_function is not None or self.selector is not None or 44 self.text is not None) 45 46 def RunAction(self, tab): 47 if not self.HasElementSelector(): 48 self.element_function = 'document.body' 49 50 tap_cmd = (''' 51 window.__tapAction.start({ 52 element: element, 53 left_position_percentage: %s, 54 top_position_percentage: %s, 55 duration_ms: %s, 56 gesture_source_type: %s 57 });''' 58 % (self.left_position_percentage, 59 self.top_position_percentage, 60 self.duration_ms, 61 self._synthetic_gesture_source)) 62 code = ''' 63 function(element, errorMsg) { 64 if (!element) { 65 throw Error('Cannot find element: ' + errorMsg); 66 } 67 %s; 68 }''' % tap_cmd 69 70 page_action.EvaluateCallbackWithElement( 71 tab, code, selector=self.selector, text=self.text, 72 element_function=self.element_function) 73 tab.WaitForJavaScriptExpression('window.__tapActionDone', 60) 74