70bdc25da827d0bb1fd89fd1b8a08f1603a10709
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / tap.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 gesture_action
7 from telemetry.page.actions import page_action
8
9 class TapAction(gesture_action.GestureAction):
10   def __init__(self, attributes=None):
11     super(TapAction, self).__init__(attributes)
12     self._SetTimelineMarkerBaseName('TapAction::RunAction')
13
14   def WillRunAction(self, page, tab):
15     for js_file in ['gesture_common.js', 'tap.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 tap gestures.
21     if not tab.EvaluateJavaScript('window.__TapAction_SupportedByBrowser()'):
22       raise page_action.PageActionNotSupported(
23           'Synthetic tap not supported for this browser')
24
25     done_callback = 'function() { window.__tapActionDone = true; }'
26     tab.ExecuteJavaScript("""
27         window.__tapActionDone = false;
28         window.__tapAction = new __TapAction(%s);"""
29         % (done_callback))
30
31   def RunGesture(self, page, tab, previous_action):
32     left_position_percentage = 0.5
33     top_position_percentage = 0.5
34     duration_ms = 0
35     if hasattr(self, 'left_position_percentage'):
36       left_position_percentage = self.left_position_percentage
37     if hasattr(self, 'top_position_percentage'):
38       top_position_percentage = self.top_position_percentage
39     if hasattr(self, 'duration_ms'):
40       duration_ms = self.duration_ms
41     if hasattr(self, 'element_function'):
42       tab.ExecuteJavaScript("""
43           (%s)(function(element) { window.__tapAction.start(
44              { element: element,
45                left_position_percentage: %s,
46                top_position_percentage: %s,
47                duration_ms: %s })
48              });""" % (self.element_function,
49                        left_position_percentage,
50                        top_position_percentage,
51                        duration_ms))
52     else:
53       tab.ExecuteJavaScript("""
54           window.__tapAction.start(
55           { element: document.body,
56             left_position_percentage: %s,
57             top_position_percentage: %s,
58             duration_ms: %s });"""
59         % (left_position_percentage,
60            top_position_percentage,
61            duration_ms))
62
63     tab.WaitForJavaScriptExpression('window.__tapActionDone', 60)
64
65   def CanBeBound(self):
66     return True
67
68   def BindMeasurementJavaScript(self, tab, start_js, stop_js):
69     # Make the tap action start and stop measurement automatically.
70     tab.ExecuteJavaScript("""
71         window.__tapAction.beginMeasuringHook = function() { %s };
72         window.__tapAction.endMeasuringHook = function() { %s };
73     """ % (start_js, stop_js))