- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / play.py
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """A Telemetry page_action that performs the "play" action on media elements.
6
7 Media elements can be specified by a selector attribute. If no selector is
8 defined then then the action attempts to play the first video element or audio
9 element on the page. A selector can also be 'all' to play all media elements.
10
11 Other attributes to use are: wait_for_playing and wait_for_ended, which forces
12 the action to wait until playing and ended events get fired respectively.
13 """
14
15 from telemetry.core import exceptions
16 from telemetry.page.actions import media_action
17 from telemetry.page.actions import page_action
18
19
20 class PlayAction(media_action.MediaAction):
21   def __init__(self, attributes=None):
22     super(PlayAction, self).__init__(attributes)
23
24   def WillRunAction(self, page, tab):
25     """Load the media metrics JS code prior to running the action."""
26     super(PlayAction, self).WillRunAction(page, tab)
27     self.LoadJS(tab, 'play.js')
28
29   def RunAction(self, page, tab, previous_action):
30     try:
31       selector = self.selector if hasattr(self, 'selector') else ''
32       tab.ExecuteJavaScript('window.__playMedia("%s");' % selector)
33       timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60
34       # Check if we need to wait for 'playing' event to fire.
35       if hasattr(self, 'wait_for_playing') and self.wait_for_playing:
36         self.WaitForEvent(tab, selector, 'playing', timeout)
37       # Check if we need to wait for 'ended' event to fire.
38       if hasattr(self, 'wait_for_ended') and self.wait_for_ended:
39         self.WaitForEvent(tab, selector, 'ended', timeout)
40     except exceptions.EvaluateException:
41       raise page_action.PageActionFailed('Cannot play media element(s) with '
42                                          'selector = %s.' % selector)