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 | 8 import os |
9 | 9 |
10 from telemetry.core import util | 10 from telemetry.core import util |
11 from telemetry.page.actions import page_action | 11 from telemetry.page.actions import page_action |
12 | 12 |
13 | 13 |
14 class MediaAction(page_action.PageAction): | 14 class MediaAction(page_action.PageAction): |
15 def WillRunAction(self, page, tab): | 15 def WillRunAction(self, page, 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 self.LoadJS(tab, 'media_action.js') |
18 | 18 |
19 def RunAction(self, page, tab, previous_action): | 19 def RunAction(self, page, tab): |
20 super(MediaAction, self).RunAction(page, tab, previous_action) | 20 super(MediaAction, self).RunAction(page, tab) |
21 | 21 |
22 def LoadJS(self, tab, js_file_name): | 22 def LoadJS(self, tab, js_file_name): |
23 """Loads and executes a JS file in the tab.""" | 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: | 24 with open(os.path.join(os.path.dirname(__file__), js_file_name)) as f: |
25 js = f.read() | 25 js = f.read() |
26 tab.ExecuteJavaScript(js) | 26 tab.ExecuteJavaScript(js) |
27 | 27 |
28 def WaitForEvent(self, tab, selector, event_name, timeout): | 28 def WaitForEvent(self, tab, selector, event_name, timeout): |
29 """Halts media action until the selector's event is fired. | 29 """Halts media action until the selector's event is fired. |
30 | 30 |
(...skipping 10 matching lines...) Expand all Loading... |
41 def HasEventCompletedOrError(self, tab, selector, event_name): | 41 def HasEventCompletedOrError(self, tab, selector, event_name): |
42 if tab.EvaluateJavaScript( | 42 if tab.EvaluateJavaScript( |
43 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)): | 43 'window.__hasEventCompleted("%s", "%s");' % (selector, event_name)): |
44 return True | 44 return True |
45 error = tab.EvaluateJavaScript('window.__error') | 45 error = tab.EvaluateJavaScript('window.__error') |
46 if error: | 46 if error: |
47 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, |
48 error) | 48 error) |
49 return True | 49 return True |
50 return False | 50 return False |
OLD | NEW |