| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 |
| 4 import os | 5 import os |
| 5 | 6 |
| 6 from telemetry.page.actions.gesture_action import GestureAction | 7 from telemetry.page.actions.gesture_action import GestureAction |
| 7 from telemetry.page.actions import page_action | 8 from telemetry.page.actions import page_action |
| 8 | 9 |
| 9 class SwipeAction(GestureAction): | 10 class SwipeAction(GestureAction): |
| 10 def __init__(self, attributes=None): | 11 def __init__(self, selector=None, text=None, element_function=None, |
| 11 super(SwipeAction, self).__init__(attributes) | 12 left_start_ratio=0.5, top_start_ratio=0.5, |
| 13 direction='left', distance=100, speed=800): |
| 14 super(SwipeAction, self).__init__(None) |
| 15 if direction not in ['down', 'up', 'left', 'right']: |
| 16 raise page_action.PageActionNotSupported( |
| 17 'Invalid swipe direction: %s' % self.direction) |
| 18 self.automatically_record_interaction = False |
| 19 self._selector = selector |
| 20 self._text = text |
| 21 self._element_function = element_function |
| 22 self._left_start_ratio = left_start_ratio |
| 23 self._top_start_ratio = top_start_ratio |
| 24 self._direction = direction |
| 25 self._distance = distance |
| 26 self._speed = speed |
| 12 | 27 |
| 13 def WillRunAction(self, tab): | 28 def WillRunAction(self, tab): |
| 14 for js_file in ['gesture_common.js', 'swipe.js']: | 29 for js_file in ['gesture_common.js', 'swipe.js']: |
| 15 with open(os.path.join(os.path.dirname(__file__), js_file)) as f: | 30 with open(os.path.join(os.path.dirname(__file__), js_file)) as f: |
| 16 js = f.read() | 31 js = f.read() |
| 17 tab.ExecuteJavaScript(js) | 32 tab.ExecuteJavaScript(js) |
| 18 | 33 |
| 19 # Fail if browser doesn't support synthetic swipe gestures. | 34 # Fail if browser doesn't support synthetic swipe gestures. |
| 20 if not tab.EvaluateJavaScript('window.__SwipeAction_SupportedByBrowser()'): | 35 if not tab.EvaluateJavaScript('window.__SwipeAction_SupportedByBrowser()'): |
| 21 raise page_action.PageActionNotSupported( | 36 raise page_action.PageActionNotSupported( |
| 22 'Synthetic swipe not supported for this browser') | 37 'Synthetic swipe not supported for this browser') |
| 23 | 38 |
| 24 if (GestureAction.GetGestureSourceTypeFromOptions(tab) == | 39 if (GestureAction.GetGestureSourceTypeFromOptions(tab) == |
| 25 'chrome.gpuBenchmarking.MOUSE_INPUT'): | 40 'chrome.gpuBenchmarking.MOUSE_INPUT'): |
| 26 raise page_action.PageActionNotSupported( | 41 raise page_action.PageActionNotSupported( |
| 27 'Swipe page action does not support mouse input') | 42 'Swipe page action does not support mouse input') |
| 28 | 43 |
| 29 if not GestureAction.IsGestureSourceTypeSupported(tab, 'touch'): | 44 if not GestureAction.IsGestureSourceTypeSupported(tab, 'touch'): |
| 30 raise page_action.PageActionNotSupported( | 45 raise page_action.PageActionNotSupported( |
| 31 'Touch input not supported for this browser') | 46 'Touch input not supported for this browser') |
| 32 | 47 |
| 33 done_callback = 'function() { window.__swipeActionDone = true; }' | 48 done_callback = 'function() { window.__swipeActionDone = true; }' |
| 34 tab.ExecuteJavaScript(""" | 49 tab.ExecuteJavaScript(""" |
| 35 window.__swipeActionDone = false; | 50 window.__swipeActionDone = false; |
| 36 window.__swipeAction = new __SwipeAction(%s);""" | 51 window.__swipeAction = new __SwipeAction(%s);""" |
| 37 % (done_callback)) | 52 % (done_callback)) |
| 38 | 53 |
| 39 def RunGesture(self, tab): | 54 def RunGesture(self, tab): |
| 40 left_start_percentage = 0.5 | 55 if (self._selector is None and self._text is None and |
| 41 top_start_percentage = 0.5 | 56 self._element_function is None): |
| 42 direction = 'left' | 57 self._element_function = 'document.body' |
| 43 distance = 100 | 58 code = ''' |
| 44 speed = 800 | 59 function(element, info) { |
| 45 if hasattr(self, 'left_start_percentage'): | 60 if (!element) { |
| 46 left_start_percentage = self.left_start_percentage | 61 throw Error('Cannot find element: ' + info); |
| 47 if hasattr(self, 'top_start_percentage'): | 62 } |
| 48 top_start_percentage = self.top_start_percentage | 63 window.__swipeAction.start({ |
| 49 if hasattr(self, 'direction'): | 64 element: element, |
| 50 direction = self.direction | 65 left_start_ratio: %s, |
| 51 if direction not in ['down', 'up', 'left', 'right']: | 66 top_start_ratio: %s, |
| 52 raise page_action.PageActionNotSupported( | |
| 53 'Invalid swipe direction: %s' % direction) | |
| 54 if hasattr(self, 'distance'): | |
| 55 distance = self.distance | |
| 56 if hasattr(self, 'speed'): | |
| 57 speed = self.speed | |
| 58 if hasattr(self, 'element_function'): | |
| 59 tab.ExecuteJavaScript(""" | |
| 60 (%s)(function(element) { window.__swipeAction.start( | |
| 61 { element: element, | |
| 62 left_start_percentage: %s, | |
| 63 top_start_percentage: %s, | |
| 64 direction: '%s', | |
| 65 distance: %s, | |
| 66 speed: %s }) | |
| 67 });""" % (self.element_function, | |
| 68 left_start_percentage, | |
| 69 top_start_percentage, | |
| 70 direction, | |
| 71 distance, | |
| 72 speed)) | |
| 73 else: | |
| 74 tab.ExecuteJavaScript(""" | |
| 75 window.__swipeAction.start( | |
| 76 { element: document.body, | |
| 77 left_start_percentage: %s, | |
| 78 top_start_percentage: %s, | |
| 79 direction: '%s', | 67 direction: '%s', |
| 80 distance: %s, | 68 distance: %s, |
| 81 speed: %s });""" | 69 speed: %s |
| 82 % (left_start_percentage, | 70 }); |
| 83 top_start_percentage, | 71 }''' % (self._left_start_ratio, |
| 84 direction, | 72 self._top_start_ratio, |
| 85 distance, | 73 self._direction, |
| 86 speed)) | 74 self._distance, |
| 87 | 75 self._speed) |
| 76 page_action.EvaluateCallbackWithElement( |
| 77 tab, code, selector=self._selector, text=self._text, |
| 78 element_function=self._element_function) |
| 88 tab.WaitForJavaScriptExpression('window.__swipeActionDone', 60) | 79 tab.WaitForJavaScriptExpression('window.__swipeActionDone', 60) |
| OLD | NEW |