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 page_action
9 class PinchAction(page_action.PageAction):
10 def __init__(self, selector=None, text=None, element_function=None,
11 left_anchor_ratio=0.5, top_anchor_ratio=0.5,
12 scale_factor=None, speed_in_pixels_per_second=800):
13 super(PinchAction, self).__init__()
14 self._selector = selector
16 self._element_function = element_function
17 self._left_anchor_ratio = left_anchor_ratio
18 self._top_anchor_ratio = top_anchor_ratio
19 self._scale_factor = scale_factor
20 self._speed = speed_in_pixels_per_second
22 if (self._selector is None and self._text is None and
23 self._element_function is None):
24 self._element_function = 'document.body'
26 def WillRunAction(self, tab):
27 for js_file in ['gesture_common.js', 'pinch.js']:
28 with open(os.path.join(os.path.dirname(__file__), js_file)) as f:
30 tab.ExecuteJavaScript(js)
32 # Fail if browser doesn't support synthetic pinch gestures.
33 if not tab.EvaluateJavaScript('window.__PinchAction_SupportedByBrowser()'):
34 raise page_action.PageActionNotSupported(
35 'Synthetic pinch not supported for this browser')
37 # TODO(dominikg): Remove once JS interface changes have rolled into stable.
38 if not tab.EvaluateJavaScript('chrome.gpuBenchmarking.newPinchInterface'):
39 raise page_action.PageActionNotSupported(
40 'This version of the browser doesn\'t support the new JS interface '
41 'for pinch gestures.')
43 if (page_action.GetGestureSourceTypeFromOptions(tab) ==
44 'chrome.gpuBenchmarking.MOUSE_INPUT'):
45 raise page_action.PageActionNotSupported(
46 'Pinch page action does not support mouse input')
48 if not page_action.IsGestureSourceTypeSupported(tab, 'touch'):
49 raise page_action.PageActionNotSupported(
50 'Touch input not supported for this browser')
52 done_callback = 'function() { window.__pinchActionDone = true; }'
53 tab.ExecuteJavaScript("""
54 window.__pinchActionDone = false;
55 window.__pinchAction = new __PinchAction(%s);"""
59 def _GetDefaultScaleFactorForPage(tab):
60 current_scale_factor = tab.EvaluateJavaScript(
61 'window.outerWidth / window.innerWidth')
62 return 3.0 / current_scale_factor
64 def RunAction(self, tab):
65 scale_factor = (self._scale_factor if self._scale_factor else
66 PinchAction._GetDefaultScaleFactorForPage(tab))
68 function(element, info) {
70 throw Error('Cannot find element: ' + info);
72 window.__pinchAction.start({
74 left_anchor_ratio: %s,
79 }''' % (self._left_anchor_ratio,
80 self._top_anchor_ratio,
83 page_action.EvaluateCallbackWithElement(
84 tab, code, selector=self._selector, text=self._text,
85 element_function=self._element_function)
86 tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60)