• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from telemetry.internal.actions import page_action
6from telemetry.internal.actions import utils
7from telemetry.util import js_template
8
9
10class PinchAction(page_action.PageAction):
11  def __init__(self, selector=None, text=None, element_function=None,
12               left_anchor_ratio=0.5, top_anchor_ratio=0.5,
13               scale_factor=None, speed_in_pixels_per_second=800,
14               synthetic_gesture_source=page_action.GESTURE_SOURCE_DEFAULT):
15    super(PinchAction, self).__init__()
16    self._selector = selector
17    self._text = text
18    self._element_function = element_function
19    self._left_anchor_ratio = left_anchor_ratio
20    self._top_anchor_ratio = top_anchor_ratio
21    self._scale_factor = scale_factor
22    self._speed = speed_in_pixels_per_second
23    self._synthetic_gesture_source = ('chrome.gpuBenchmarking.%s_INPUT' %
24                                      synthetic_gesture_source)
25
26    if (self._selector is None and self._text is None and
27        self._element_function is None):
28      self._element_function = 'document.body'
29
30  def WillRunAction(self, tab):
31    utils.InjectJavaScript(tab, 'gesture_common.js')
32    utils.InjectJavaScript(tab, 'pinch.js')
33
34    # Fail if browser doesn't support synthetic pinch gestures.
35    if not tab.EvaluateJavaScript('window.__PinchAction_SupportedByBrowser()'):
36      raise page_action.PageActionNotSupported(
37          'Synthetic pinch not supported for this browser')
38
39    tab.ExecuteJavaScript("""
40        window.__pinchActionDone = false;
41        window.__pinchAction = new __PinchAction(function() {
42          window.__pinchActionDone = true;
43        });""")
44
45  @staticmethod
46  def _GetDefaultScaleFactorForPage(tab):
47    current_scale_factor = tab.EvaluateJavaScript(
48        'window.outerWidth / window.innerWidth')
49    return 3.0 / current_scale_factor
50
51  def RunAction(self, tab):
52    scale_factor = (self._scale_factor if self._scale_factor else
53                    PinchAction._GetDefaultScaleFactorForPage(tab))
54    code = js_template.Render('''
55        function(element, info) {
56          if (!element) {
57            throw Error('Cannot find element: ' + info);
58          }
59          window.__pinchAction.start({
60            element: element,
61            left_anchor_ratio: {{ left_anchor_ratio }},
62            top_anchor_ratio: {{ top_anchor_ratio }},
63            scale_factor: {{ scale_factor }},
64            speed: {{ speed }}
65          });
66        }''',
67        left_anchor_ratio=self._left_anchor_ratio,
68        top_anchor_ratio=self._top_anchor_ratio,
69        scale_factor=scale_factor,
70        speed=self._speed)
71    page_action.EvaluateCallbackWithElement(
72        tab, code, selector=self._selector, text=self._text,
73        element_function=self._element_function)
74    tab.WaitForJavaScriptCondition('window.__pinchActionDone', timeout=60)
75