pinch.py revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 PinchAction(GestureAction): 10 def __init__(self, attributes=None): 11 super(PinchAction, self).__init__(attributes) 12 13 def WillRunAction(self, page, tab): 14 for js_file in ['gesture_common.js', 'pinch.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 pinch gestures. 20 if not tab.EvaluateJavaScript('window.__PinchAction_SupportedByBrowser()'): 21 raise page_action.PageActionNotSupported( 22 'Synthetic pinch not supported for this browser') 23 24 if (GestureAction.GetGestureSourceTypeFromOptions(tab) == 25 'chrome.gpuBenchmarking.MOUSE_INPUT'): 26 raise page_action.PageActionNotSupported( 27 'Pinch 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.__pinchActionDone = true; }' 34 tab.ExecuteJavaScript(""" 35 window.__pinchActionDone = false; 36 window.__pinchAction = new __PinchAction(%s);""" 37 % done_callback) 38 39 def RunGesture(self, page, tab): 40 left_anchor_percentage = getattr(self, 'left_anchor_percentage', 0.5) 41 top_anchor_percentage = getattr(self, 'top_anchor_percentage', 0.5) 42 zoom_in = getattr(self, 'zoom_in', True) 43 pixels_to_cover = getattr(self, 'pixels_to_cover', 500) 44 speed = getattr(self, 'speed', 800) 45 46 if hasattr(self, 'element_function'): 47 tab.ExecuteJavaScript(""" 48 (%s)(function(element) { window.__pinchAction.start( 49 { element: element, 50 left_anchor_percentage: %s, 51 top_anchor_percentage: %s, 52 zoom_in: %s, 53 pixels_to_cover: %s, 54 speed: %s }) 55 });""" % (self.element_function, 56 left_anchor_percentage, 57 top_anchor_percentage, 58 'true' if zoom_in else 'false', 59 pixels_to_cover, 60 speed)) 61 else: 62 tab.ExecuteJavaScript(""" 63 window.__pinchAction.start( 64 { element: document.body, 65 left_anchor_percentage: %s, 66 top_anchor_percentage: %s, 67 zoom_in: %s, 68 pixels_to_cover: %s, 69 speed: %s });""" 70 % (left_anchor_percentage, 71 top_anchor_percentage, 72 'true' if zoom_in else 'false', 73 pixels_to_cover, 74 speed)) 75 76 tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60) 77 78 def CanBeBound(self): 79 return True 80 81 def BindMeasurementJavaScript(self, tab, start_js, stop_js): 82 # Make the pinch action start and stop measurement automatically. 83 tab.ExecuteJavaScript(""" 84 window.__pinchAction.beginMeasuringHook = function() { %s }; 85 window.__pinchAction.endMeasuringHook = function() { %s }; 86 """ % (start_js, stop_js)) 87