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 import os | |
9 | 8 |
10 from telemetry.core import util | 9 from telemetry.core import util |
11 from telemetry.internal.actions import page_action | 10 from telemetry.internal.actions import page_action |
| 11 from telemetry.internal.actions import utils |
12 | 12 |
13 | 13 |
14 class MediaAction(page_action.PageAction): | 14 class MediaAction(page_action.PageAction): |
15 def WillRunAction(self, tab): | 15 def WillRunAction(self, tab): |
16 """Loads the common media action JS code prior to running the action.""" | 16 """Loads the common media action JS code prior to running the action.""" |
17 self.LoadJS(tab, 'media_action.js') | 17 utils.InjectJavaScript(tab, 'media_action.js') |
18 | 18 |
19 def RunAction(self, tab): | 19 def RunAction(self, tab): |
20 super(MediaAction, self).RunAction(tab) | 20 super(MediaAction, self).RunAction(tab) |
21 | 21 |
22 def LoadJS(self, tab, js_file_name): | |
23 """Loads and executes a JS file in the tab.""" | |
24 with open(os.path.join(os.path.dirname(__file__), js_file_name)) as f: | |
25 js = f.read() | |
26 tab.ExecuteJavaScript(js) | |
27 | |
28 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds): | 22 def WaitForEvent(self, tab, selector, event_name, timeout_in_seconds): |
29 """Halts media action until the selector's event is fired. | 23 """Halts media action until the selector's event is fired. |
30 | 24 |
31 Args: | 25 Args: |
32 tab: The tab to check for event on. | 26 tab: The tab to check for event on. |
33 selector: Media element selector. | 27 selector: Media element selector. |
34 event_name: Name of the event to check if fired or not. | 28 event_name: Name of the event to check if fired or not. |
35 timeout_in_seconds: Timeout to check for event, throws an exception if | 29 timeout_in_seconds: Timeout to check for event, throws an exception if |
36 not fired. | 30 not fired. |
37 """ | 31 """ |
38 util.WaitFor(lambda: | 32 util.WaitFor(lambda: |
39 self.HasEventCompletedOrError(tab, selector, event_name), | 33 self.HasEventCompletedOrError(tab, selector, event_name), |
40 timeout=timeout_in_seconds) | 34 timeout=timeout_in_seconds) |
41 | 35 |
42 def HasEventCompletedOrError(self, tab, selector, event_name): | 36 def HasEventCompletedOrError(self, tab, selector, event_name): |
43 if tab.EvaluateJavaScript( | 37 if tab.EvaluateJavaScript( |
44 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)): | 38 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)): |
45 return True | 39 return True |
46 error = tab.EvaluateJavaScript('window.__error') | 40 error = tab.EvaluateJavaScript('window.__error') |
47 if error: | 41 if error: |
48 logging.error('Detected media error while waiting for %s: %s', event_name, | 42 logging.error('Detected media error while waiting for %s: %s', event_name, |
49 error) | 43 error) |
50 return True | 44 return True |
51 return False | 45 return False |
OLD | NEW |