seek.py revision 1e9bf3e0803691d0a228da41fc608347b6db4340
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer# Copyright 2013 The Chromium Authors. All rights reserved.
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer# Use of this source code is governed by a BSD-style license that can be
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer# found in the LICENSE file.
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner"""A Telemetry page_action that performs the "seek" action on media elements.
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerAction attributes are:
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer- seek_time: The media time to seek to. Test fails if not provided.
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer- selector: If no selector is defined then the action attempts to seek the first
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            media element on the page. If 'all' then seek all media elements.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer- log_seek_time: If true the seek time is recorded, otherwise media measurement
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                 will not be aware of the seek action. Used to perform multiple
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                 seeks. Default true.
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer- wait_for_seeked: If true forces the action to wait for seeked event to fire.
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                   Default false.
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer- wait_timeout: Timeout to wait for seeked event. Only valid with
172e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor                wait_for_seeked=true
18e7d07d113677a39026ff5119b8b67f6fe8ca9793Ted Kremenek- seek_label: A suffix string to name the seek perf measurement.
199f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor"""
20ad75653f81dece1c806e9c28dd7e7582c9929a27Ted Kremenek
21ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregorfrom telemetry.core import exceptions
22d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregorfrom telemetry.page.actions import media_action
237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregorfrom telemetry.page.actions import page_action
24464175bba1318bef7905122e9fda20cff926df78Chris Lattner
2550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor
26464175bba1318bef7905122e9fda20cff926df78Chris Lattnerclass SeekAction(media_action.MediaAction):
2768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  def WillRunAction(self, page, tab):
282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    """Load the media metrics JS code prior to running the action."""
296c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner    super(SeekAction, self).WillRunAction(page, tab)
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    self.LoadJS(tab, 'seek.js')
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  def RunAction(self, page, tab, previous_action):
33b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner    try:
34b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner      assert hasattr(self, 'seek_time')
35b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner      selector = self.selector if hasattr(self, 'selector') else ''
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      log_seek = self.log_seek == True if hasattr(self, 'log_seek') else True
37a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner      seek_label = self.seek_label if hasattr(self, 'seek_label') else ''
38e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar      tab.ExecuteJavaScript('window.__seekMedia("%s", "%s", %i, "%s");' %
39e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                            (selector, self.seek_time, log_seek, seek_label))
402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60
41c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner      # Check if we need to wait for 'seeked' event to fire.
42e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar      if hasattr(self, 'wait_for_seeked') and self.wait_for_seeked:
43e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar        self.WaitForEvent(tab, selector, 'seeked', timeout)
44d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor    except exceptions.EvaluateException:
45e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar      raise page_action.PageActionFailed('Cannot seek media element(s) with '
46e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                                         'selector = %s.' % selector)
470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson