Chromium Code Reviews| Index: tools/telemetry/telemetry/page/actions/drag.py |
| diff --git a/tools/telemetry/telemetry/page/actions/drag.py b/tools/telemetry/telemetry/page/actions/drag.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..336d4beb0e01391799135801b5225a8e1b277c19 |
| --- /dev/null |
| +++ b/tools/telemetry/telemetry/page/actions/drag.py |
| @@ -0,0 +1,87 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
|
petrcermak
2015/02/25 11:35:49
Could you provide a short module or class docstrin
ssid
2015/02/27 11:35:15
Done.
|
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import os |
|
petrcermak
2015/02/25 11:35:49
There should be a blank line before this line.
ssid
2015/02/27 11:35:15
Done.
|
| + |
| +from telemetry.page.actions import page_action |
| + |
| + |
| +class DragAction(page_action.PageAction): |
| + # TODO(chrishenry): Ignore attributes, to be deleted when usage in |
|
petrcermak
2015/02/25 11:35:49
I don't think you should add a TODO for someone el
ssid
2015/02/27 11:35:15
Done.
|
| + # other repo is cleaned up. |
| + def __init__(self, selector=None, text=None, element_function=None, |
| + left_start_ratio=None, top_start_ratio=None, left_end_ratio=None, |
| + top_end_ratio=None, speed_in_pixels_per_second=800, |
| + use_touch=False): |
| + super(DragAction, self).__init__() |
| + self._selector = selector |
| + self._text = text |
| + self._element_function = element_function |
| + self._left_start_ratio = left_start_ratio |
| + self._top_start_ratio = top_start_ratio |
| + self._left_end_ratio = left_end_ratio |
| + self._top_end_ratio = top_end_ratio |
| + self._speed = speed_in_pixels_per_second |
| + self._use_touch = use_touch |
| + |
| + def WillRunAction(self, tab): |
| + for js_file in ['gesture_common.js', 'drag.js']: |
| + with open(os.path.join(os.path.dirname(__file__), js_file)) as f: |
| + js = f.read() |
| + tab.ExecuteJavaScript(js) |
| + |
| + # Fail if browser doesn't support synthetic drag gestures. |
| + if not tab.EvaluateJavaScript('window.__DragAction_SupportedByBrowser()'): |
| + raise page_action.PageActionNotSupported( |
| + 'Synthetic drag not supported for this browser') |
| + |
| + # Fail if this action requires touch and we can't send touch events. |
| + if self._use_touch: |
| + if not page_action.IsGestureSourceTypeSupported(tab, 'touch'): |
| + raise page_action.PageActionNotSupported( |
| + 'Touch drag not supported for this browser') |
| + |
| + if (page_action.GetGestureSourceTypeFromOptions(tab) == |
| + 'chrome.gpuBenchmarking.MOUSE_INPUT'): |
| + raise page_action.PageActionNotSupported( |
| + 'Drag requires touch on this page but mouse input was requested') |
| + |
| + done_callback = 'function() { window.__dragActionDone = true; }' |
| + tab.ExecuteJavaScript(""" |
|
petrcermak
2015/02/25 11:35:48
You're mixing single and double quotes in this fil
ssid
2015/02/27 11:35:15
Double quotes are needed at few places.
|
| + window.__dragActionDone = false; |
| + window.__dragAction = new __DragAction(%s);""" |
| + % (done_callback)) |
|
petrcermak
2015/02/25 11:35:49
Firstly, you don't need the brackets around "done_
ssid
2015/02/27 11:35:15
It is separated because the line becomes too large
|
| + |
| + def RunAction(self, tab): |
| + if (self._selector is None and self._text is None and |
|
petrcermak
2015/02/25 11:35:49
Do you expect that these could be empty (i.e. '')?
ssid
2015/02/27 11:35:15
Maybe if someone wants to test with empty string.
|
| + self._element_function is None): |
| + self._element_function = 'document.body' |
| + |
| + if self._use_touch: |
| + gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' |
|
petrcermak
2015/02/25 11:35:49
If you wanted to shorten this, you could do the fo
ssid
2015/02/27 11:35:15
This looks more readable, I think.
|
| + else: |
| + gesture_source_type = 'chrome.gpuBenchmarking.MOUSE_INPUT' |
| + code = ''' |
| + function(element, info) { |
| + if (!element) { |
| + throw Error('Cannot find element: ' + info); |
| + } |
| + window.__dragAction.start({ |
| + element: element, |
| + left_start_ratio: %s, |
| + top_start_ratio: %s, |
| + left_end_ratio: %s, |
| + top_end_ratio: %s, |
| + speed: %s, |
| + gesture_source_type: %s |
| + }); |
| + }''' % (self._left_start_ratio, |
| + self._top_start_ratio, |
| + self._left_end_ratio, |
| + self._top_end_ratio, |
| + self._speed, |
| + gesture_source_type) |
| + page_action.EvaluateCallbackWithElement( |
| + tab, code, selector=self._selector, text=self._text, |
| + element_function=self._element_function) |
| + tab.WaitForJavaScriptExpression('window.__dragActionDone', 60) |