Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / pinch.py
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 import os
5
6 from telemetry.page.actions.gesture_action import GestureAction
7 from telemetry.page.actions import page_action
8
9 class PinchAction(GestureAction):
10   def __init__(self, attributes=None):
11     super(PinchAction, self).__init__(attributes)
12     self._SetTimelineMarkerBaseName('PinchAction::RunAction')
13
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:
17         js = f.read()
18         tab.ExecuteJavaScript(js)
19
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')
24
25     if (GestureAction.GetGestureSourceTypeFromOptions(tab) ==
26         'chrome.gpuBenchmarking.MOUSE_INPUT'):
27       raise page_action.PageActionNotSupported(
28           'Pinch page action does not support mouse input')
29
30     done_callback = 'function() { window.__pinchActionDone = true; }'
31     tab.ExecuteJavaScript("""
32         window.__pinchActionDone = false;
33         window.__pinchAction = new __PinchAction(%s);"""
34         % done_callback)
35
36   def RunGesture(self, page, tab, previous_action):
37     zoom_in = True
38     if hasattr(self, 'zoom_in'):
39       zoom_in = self.zoom_in
40
41     pixels_to_move = 4000
42     if hasattr(self, 'pixels_to_move'):
43       pixels_to_move = self.pixels_to_move
44
45     tab.ExecuteJavaScript('window.__pinchAction.start(%s, %f)'
46                           % ("true" if zoom_in else "false", pixels_to_move))
47
48     tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60)
49
50   def CanBeBound(self):
51     return True
52
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))