OLD | NEW |
| (Empty) |
1 # Copyright 2015 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 | |
5 """A Telemetry page_action that performs the "drag" action on pages. | |
6 | |
7 Action parameters are: | |
8 - selector: If no selector is defined then the action attempts to drag the | |
9 document element on the page. | |
10 - element_function: CSS selector used to evaluate callback when test completes | |
11 - text: The element with exact text is selected. | |
12 - left_start_ratio: ratio of start point's left coordinate to the element | |
13 width. | |
14 - top_start_ratio: ratio of start point's top coordinate to the element height. | |
15 - left_end_ratio: ratio of end point's left coordinate to the element width. | |
16 - left_end_ratio: ratio of end point's top coordinate to the element height. | |
17 - speed_in_pixels_per_second: speed of the drag gesture in pixels per second. | |
18 - use_touch: boolean value to specify if gesture should use touch input or not. | |
19 """ | |
20 | |
21 import os | |
22 | |
23 from telemetry.page.actions import page_action | |
24 | |
25 | |
26 class DragAction(page_action.PageAction): | |
27 | |
28 def __init__(self, selector=None, text=None, element_function=None, | |
29 left_start_ratio=None, top_start_ratio=None, left_end_ratio=None, | |
30 top_end_ratio=None, speed_in_pixels_per_second=800, | |
31 use_touch=False): | |
32 super(DragAction, self).__init__() | |
33 self._selector = selector | |
34 self._text = text | |
35 self._element_function = element_function | |
36 self._left_start_ratio = left_start_ratio | |
37 self._top_start_ratio = top_start_ratio | |
38 self._left_end_ratio = left_end_ratio | |
39 self._top_end_ratio = top_end_ratio | |
40 self._speed = speed_in_pixels_per_second | |
41 self._use_touch = use_touch | |
42 | |
43 def WillRunAction(self, tab): | |
44 for js_file in ['gesture_common.js', 'drag.js']: | |
45 with open(os.path.join(os.path.dirname(__file__), js_file)) as f: | |
46 js = f.read() | |
47 tab.ExecuteJavaScript(js) | |
48 | |
49 # Fail if browser doesn't support synthetic drag gestures. | |
50 if not tab.EvaluateJavaScript('window.__DragAction_SupportedByBrowser()'): | |
51 raise page_action.PageActionNotSupported( | |
52 'Synthetic drag not supported for this browser') | |
53 | |
54 # Fail if this action requires touch and we can't send touch events. | |
55 if self._use_touch: | |
56 if not page_action.IsGestureSourceTypeSupported(tab, 'touch'): | |
57 raise page_action.PageActionNotSupported( | |
58 'Touch drag not supported for this browser') | |
59 | |
60 if (page_action.GetGestureSourceTypeFromOptions(tab) == | |
61 'chrome.gpuBenchmarking.MOUSE_INPUT'): | |
62 raise page_action.PageActionNotSupported( | |
63 'Drag requires touch on this page but mouse input was requested') | |
64 | |
65 done_callback = 'function() { window.__dragActionDone = true; }' | |
66 tab.ExecuteJavaScript(''' | |
67 window.__dragActionDone = false; | |
68 window.__dragAction = new __DragAction(%s);''' | |
69 % done_callback) | |
70 | |
71 def RunAction(self, tab): | |
72 if (self._selector is None and self._text is None and | |
73 self._element_function is None): | |
74 self._element_function = 'document.body' | |
75 | |
76 gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' | |
77 if (page_action.IsGestureSourceTypeSupported(tab, 'mouse') and | |
78 not self._use_touch): | |
79 gesture_source_type = 'chrome.gpuBenchmarking.MOUSE_INPUT' | |
80 | |
81 code = ''' | |
82 function(element, info) { | |
83 if (!element) { | |
84 throw Error('Cannot find element: ' + info); | |
85 } | |
86 window.__dragAction.start({ | |
87 element: element, | |
88 left_start_ratio: %s, | |
89 top_start_ratio: %s, | |
90 left_end_ratio: %s, | |
91 top_end_ratio: %s, | |
92 speed: %s, | |
93 gesture_source_type: %s | |
94 }); | |
95 }''' % (self._left_start_ratio, | |
96 self._top_start_ratio, | |
97 self._left_end_ratio, | |
98 self._top_end_ratio, | |
99 self._speed, | |
100 gesture_source_type) | |
101 page_action.EvaluateCallbackWithElement( | |
102 tab, code, selector=self._selector, text=self._text, | |
103 element_function=self._element_function) | |
104 tab.WaitForJavaScriptExpression('window.__dragActionDone', 60) | |
OLD | NEW |