Upstream version 9.38.198.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 import page_action
7
8
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
15     self._text = text
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
21
22     if (self._selector is None and self._text is None and
23         self._element_function is None):
24       self._element_function = 'document.body'
25
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:
29         js = f.read()
30         tab.ExecuteJavaScript(js)
31
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')
36
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.')
42
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')
47
48     if not page_action.IsGestureSourceTypeSupported(tab, 'touch'):
49       raise page_action.PageActionNotSupported(
50           'Touch input not supported for this browser')
51
52     done_callback = 'function() { window.__pinchActionDone = true; }'
53     tab.ExecuteJavaScript("""
54         window.__pinchActionDone = false;
55         window.__pinchAction = new __PinchAction(%s);"""
56         % done_callback)
57
58   @staticmethod
59   def _GetDefaultScaleFactorForPage(tab):
60     current_scale_factor = tab.EvaluateJavaScript(
61         'window.outerWidth / window.innerWidth')
62     return 3.0 / current_scale_factor
63
64   def RunAction(self, tab):
65     scale_factor = (self._scale_factor if self._scale_factor else
66                     PinchAction._GetDefaultScaleFactorForPage(tab))
67     code = '''
68         function(element, info) {
69           if (!element) {
70             throw Error('Cannot find element: ' + info);
71           }
72           window.__pinchAction.start({
73             element: element,
74             left_anchor_ratio: %s,
75             top_anchor_ratio: %s,
76             scale_factor: %s,
77             speed: %s
78           });
79         }''' % (self._left_anchor_ratio,
80                 self._top_anchor_ratio,
81                 scale_factor,
82                 self._speed)
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)