| 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 |
| 5 """Common media action functions.""" | 5 """Common media action functions.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 from telemetry.internal.actions import page_action | 9 from telemetry.internal.actions import page_action |
| 10 from telemetry.internal.actions import utils | 10 from telemetry.internal.actions import utils |
| 11 from telemetry.util import js_template |
| 11 | 12 |
| 12 import py_utils | 13 import py_utils |
| 13 | 14 |
| 14 | 15 |
| 15 class MediaAction(page_action.PageAction): | 16 class MediaAction(page_action.PageAction): |
| 16 def WillRunAction(self, tab): | 17 def WillRunAction(self, tab): |
| 17 """Loads the common media action JS code prior to running the action.""" | 18 """Loads the common media action JS code prior to running the action.""" |
| 18 utils.InjectJavaScript(tab, 'media_action.js') | 19 utils.InjectJavaScript(tab, 'media_action.js') |
| 19 | 20 |
| 20 def RunAction(self, tab): | 21 def RunAction(self, tab): |
| 21 super(MediaAction, self).RunAction(tab) | 22 super(MediaAction, self).RunAction(tab) |
| 22 | 23 |
| 23 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds): | 24 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds): |
| 24 """Halts media action until the selector's event is fired. | 25 """Halts media action until the selector's event is fired. |
| 25 | 26 |
| 26 Args: | 27 Args: |
| 27 tab: The tab to check for event on. | 28 tab: The tab to check for event on. |
| 28 selector: Media element selector. | 29 selector: Media element selector. |
| 29 event_name: Name of the event to check if fired or not. | 30 event_name: Name of the event to check if fired or not. |
| 30 timeout_in_seconds: Timeout to check for event, throws an exception if | 31 timeout_in_seconds: Timeout to check for event, throws an exception if |
| 31 not fired. | 32 not fired. |
| 32 """ | 33 """ |
| 33 py_utils.WaitFor( | 34 py_utils.WaitFor( |
| 34 lambda: self.HasEventCompletedOrError(tab, selector, event_name), | 35 lambda: self.HasEventCompletedOrError(tab, selector, event_name), |
| 35 timeout=timeout_in_seconds) | 36 timeout=timeout_in_seconds) |
| 36 | 37 |
| 37 def HasEventCompletedOrError(self, tab, selector, event_name): | 38 def HasEventCompletedOrError(self, tab, selector, event_name): |
| 38 # TODO(catapult:#3028): Fix interpolation of JavaScript values. | 39 # TODO(catapult:#3028): Render in JavaScript method when supported by API. |
| 39 if tab.EvaluateJavaScript( | 40 code = js_template.Render( |
| 40 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)): | 41 'window.__hasEventCompleted({{ selector }}, {{ event_name }});', |
| 42 selector=selector, event_name=event_name) |
| 43 if tab.EvaluateJavaScript(code): |
| 41 return True | 44 return True |
| 42 error = tab.EvaluateJavaScript('window.__error') | 45 error = tab.EvaluateJavaScript('window.__error') |
| 43 if error: | 46 if error: |
| 44 logging.error('Detected media error while waiting for %s: %s', event_name, | 47 logging.error('Detected media error while waiting for %s: %s', event_name, |
| 45 error) | 48 error) |
| 46 return True | 49 return True |
| 47 return False | 50 return False |
| OLD | NEW |