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.gesture_action import GestureAction
7 from telemetry.page.actions import page_action
9 class PinchAction(GestureAction):
10 def __init__(self, attributes=None):
11 super(PinchAction, self).__init__(attributes)
12 self._SetTimelineMarkerBaseName('PinchAction::RunAction')
14 def WillRunAction(self, page, tab):
15 for js_file in ['gesture_common.js', 'pinch.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 pinch gestures.
21 if not tab.EvaluateJavaScript('window.__PinchAction_SupportedByBrowser()'):
22 raise page_action.PageActionNotSupported(
23 'Synthetic pinch not supported for this browser')
25 if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
26 'chrome.gpuBenchmarking.MOUSE_INPUT'):
27 raise page_action.PageActionNotSupported(
28 'Pinch page action does not support mouse input')
30 done_callback = 'function() { window.__pinchActionDone = true; }'
31 tab.ExecuteJavaScript("""
32 window.__pinchActionDone = false;
33 window.__pinchAction = new __PinchAction(%s);"""
36 def RunGesture(self, page, tab, previous_action):
38 if hasattr(self, 'zoom_in'):
39 zoom_in = self.zoom_in
42 if hasattr(self, 'pixels_to_move'):
43 pixels_to_move = self.pixels_to_move
45 tab.ExecuteJavaScript('window.__pinchAction.start(%s, %f)'
46 % ("true" if zoom_in else "false", pixels_to_move))
48 tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60)
53 def BindMeasurementJavaScript(self, tab, start_js, stop_js):
54 # Make the pinch action start and stop measurement automatically.
55 tab.ExecuteJavaScript("""
56 window.__pinchAction.beginMeasuringHook = function() { %s };
57 window.__pinchAction.endMeasuringHook = function() { %s };
58 """ % (start_js, stop_js))