Home | History | Annotate | Download | only in video_YouTubeMseEme
      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 import logging, os
      6 
      7 from autotest_lib.client.bin import test, utils
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.client.common_lib.cros import chrome
     10 from autotest_lib.client.cros import httpd
     11 
     12 
     13 class video_YouTubeMseEme(test.test):
     14     """The main test class for MSE/EME.
     15 
     16     """
     17 
     18 
     19     version = 1
     20 
     21     PLAYER_PAGE = 'http://localhost:8000/files/video_YouTubeMseEme.html'
     22     TEST_JS = 'files/video_YouTubeMseEme.js'
     23 
     24     POLLING_TIME = 0.1
     25 
     26 
     27     def init(self, chrome, player_page):
     28         """Initialization function for this test class.
     29 
     30         @param chrome: An Autotest Chrome instance.
     31         @param player_page: Dummy HTML file to load.
     32 
     33         """
     34         self._testServer = httpd.HTTPListener(8000, docroot=self.bindir)
     35         self._testServer.run()
     36 
     37         self.tab = chrome.browser.tabs[0]
     38         self.tab.Navigate(player_page)
     39         self.tab.WaitForDocumentReadyStateToBeComplete()
     40         self.load_javascript(self.TEST_JS)
     41 
     42 
     43     def _check_event_happened(self, event_name):
     44         """A wrapper to check if an event in JS has fired.
     45 
     46         @param event_name: A string to denote the name of the event to check.
     47         """
     48         self.tab.WaitForJavaScriptExpression(
     49              'window.__eventReporter["%s"] === true;' % event_name, 5)
     50 
     51 
     52     def load_javascript(self, sub_path):
     53         """A wrapper to load a JS file into the current tab.
     54 
     55         @param sub_path: The relative path from the current .py file.
     56 
     57         """
     58         full_path = os.path.join(os.path.dirname(__file__), sub_path)
     59         with open(full_path) as f:
     60             js = f.read()
     61             self.tab.ExecuteJavaScript(js)
     62             logging.info('Loaded accompanying .js script.')
     63 
     64 
     65     def get_test_state(self, test_name, delay_time_sec=5):
     66         """A wrapper to check the state of a test in the accompanying JS.
     67 
     68         @param test_name: The name of the test that was ran.
     69         @param delay_time_sec: Time to wait before querying the test (float).
     70                 This is to give the VM some time to schedule the next execution.
     71 
     72         @returns: A boolean value indicating the success of the test.
     73 
     74         """
     75         def _test_passed_condition(test_name):
     76             """An inner function to test if the test passed.
     77 
     78             @param test_name: The name of the test that was ran.
     79 
     80             @returns: A boolean indicating if the test passed.
     81 
     82             """
     83             return self.tab.EvaluateJavaScript(
     84                     'window.__testState["%s"];' % test_name)
     85 
     86         return utils.poll_for_condition(
     87                 lambda: _test_passed_condition(test_name),
     88                 timeout=delay_time_sec,
     89                 desc=test_name)
     90 
     91 
     92     def test_media_source_presence(self):
     93         """Tests for the existence of the Media Source Extension.
     94 
     95         """
     96         self.assert_(
     97                 self.tab.EvaluateJavaScript(
     98                         'window.WebKitMediaSource !== undefined'),
     99                 msg='test_media_source_presence failed.')
    100 
    101 
    102     def test_attach_source(self):
    103         """Tests if attaching a the MediaSource to the video tag is successful.
    104 
    105         """
    106         self.tab.ExecuteJavaScript('window.__testAttach();')
    107         self._check_event_happened('sourceopen')
    108 
    109 
    110     def test_add_source_buffer(self):
    111         """Tests adding the source buffer to the MediaSource is successful.
    112 
    113         """
    114         self.tab.ExecuteJavaScript('window.__testAddSourceBuffer();')
    115         self.assert_(
    116                 self.get_test_state('addSourceBuffer'),
    117                 msg='test_add_source_buffer failed.')
    118 
    119 
    120     def test_add_supported_formats(self):
    121         """Tests adding supported formats to the MediaSource is successful.
    122 
    123         """
    124         self.tab.ExecuteJavaScript('window.__testAddSupportedFormats();')
    125         self.assert_(
    126                 self.get_test_state('addSupportedFormats'),
    127                 msg='test_add_supported_formats failed.')
    128 
    129 
    130     def test_add_source_buffer_exception(self):
    131         """Tests adding the source buffer to an uninitialized MediaSource.
    132 
    133         """
    134         self.tab.ExecuteJavaScript(
    135                 'window.__testAddSourceBufferException();')
    136         self.assert_(
    137                 self.get_test_state('addSourceBufferException'),
    138                 msg='test_add_source_buffer_exception failed.')
    139 
    140 
    141     def test_initial_video_state(self):
    142         """Tests the initial states of the video object.
    143 
    144         """
    145         self.tab.ExecuteJavaScript('window.__testInitialVideoState();')
    146         self.assert_(
    147                 self.get_test_state('initialVideoState'),
    148                 msg='test_initial_video_state failed.')
    149 
    150 
    151     def test_initial_media_source_state(self):
    152         """Tests the initial states of the MediaSource object.
    153 
    154         """
    155         self.tab.ExecuteJavaScript('window.__testInitialMSState();')
    156         self.assert_(
    157                 self.get_test_state('initialMSState'),
    158                 msg='test_initial_media_source_state failed.')
    159 
    160 
    161     def test_append(self):
    162         """Tests appending audio and video streams.
    163 
    164         """
    165         self.tab.ExecuteJavaScript('window.__testAppend_audio();')
    166         self.assert_(
    167                 self.get_test_state('append_audio'),
    168                 msg='test_append failed on audio append.')
    169 
    170         self.tab.ExecuteJavaScript('window.__testAppend_video();')
    171         self.assert_(
    172                 self.get_test_state('append_video'),
    173                 msg='test_append failed on video append.')
    174 
    175 
    176     def test_append_abort(self):
    177         """Tests appending followed by aborting audio and video streams.
    178 
    179         """
    180         self.tab.ExecuteJavaScript('window.__testAppendAbort_audio();')
    181         self.assert_(
    182                 self.get_test_state('appendAbort_audio'),
    183                 msg='test_append_abort failed on audio.')
    184 
    185         self.tab.ExecuteJavaScript('window.__testAppendAbort_video();')
    186         self.assert_(
    187                 self.get_test_state('appendAbort_video'),
    188                 msg='test_append_abort failed on video.')
    189 
    190 
    191     def test_append_timestamp_offset(self):
    192         """Tests appending with a timestamp offset.
    193 
    194         """
    195         self.tab.ExecuteJavaScript(
    196                 'window.__testAppendTimestampOffset_audio();')
    197         self.assert_(
    198                 self.get_test_state('appendTimestampOffset_audio'),
    199                 msg='test_append_timestamp_offset failed on audio.')
    200 
    201         self.tab.ExecuteJavaScript(
    202                 'window.__testAppendTimestampOffset_video();')
    203         self.assert_(
    204                 self.get_test_state('appendTimestampOffset_video'),
    205                 msg='test_append_timestamp_offset failed on video.')
    206 
    207 
    208     def test_duration(self):
    209         """Tests if it's possible to set the duration on the Media Source.
    210 
    211         """
    212         self.tab.ExecuteJavaScript('window.__testDuration();')
    213         self.assert_(
    214                 self.get_test_state('duration'),
    215                 msg='test_duration failed.')
    216 
    217 
    218     def test_duration_after_append(self):
    219         """Tests if the duration changes after appending to the source buffer.
    220 
    221         """
    222         self.tab.ExecuteJavaScript('window.__testDurationAfterAppend_audio();')
    223         self.assert_(
    224                 self.get_test_state('durationAfterAppend_audio'),
    225                 msg='test_duration_after_append failed on audio.')
    226 
    227         self.tab.ExecuteJavaScript('window.__testDurationAfterAppend_video();')
    228         self.assert_(
    229                 self.get_test_state('durationAfterAppend_video'),
    230                 msg='test_duration_after_append failed on video.')
    231 
    232 
    233     def test_source_remove(self):
    234         """Tests if removing the source buffers works correctly.
    235 
    236         """
    237         self.tab.ExecuteJavaScript('window.__testSourceRemove();')
    238         self.assert_(
    239                 self.get_test_state('sourceRemove'),
    240                 msg='test_source_remove failed.')
    241 
    242 
    243     def test_can_play_webm(self):
    244         """Tests if it's possible to play WebM content.
    245 
    246         """
    247         self.assert_(
    248                 self.tab.EvaluateJavaScript('window.__testCanPlayWebM();'),
    249                 msg='test_can_play_webm failed.')
    250 
    251 
    252     def test_can_play_clear_key(self):
    253         """Tests if it's possible to play ClearKey content.
    254 
    255         """
    256         self.assert_(
    257                 self.tab.EvaluateJavaScript('window.__testCanPlayClearKey();'),
    258                 msg='test_can_play_clear_key failed.')
    259 
    260 
    261     def test_can_not_play_play_ready(self):
    262         """Tests if it's impossible to play PlayReady.
    263 
    264         """
    265         self.assert_(
    266                 self.tab.EvaluateJavaScript(
    267                         'window.__testCanNotPlayPlayReady();'),
    268                 msg='test_can_not_play_play_ready failed.')
    269 
    270 
    271     def test_can_play_widevine(self):
    272         """Tests if it's possible to play Widevine content.
    273 
    274         """
    275         self.assert_(
    276                 self.tab.EvaluateJavaScript('window.__testCanPlayWidevine();'),
    277                 msg='test_can_play_widevine failed.')
    278 
    279 
    280     def run_once(self, subtest_name):
    281         with chrome.Chrome() as cr:
    282             self.init(cr, self.PLAYER_PAGE)
    283 
    284             try:
    285                 # The control file passes in a test name, which is the name of
    286                 #  the test to run, prepended with 'test_'.
    287                 function_to_call = getattr(self, 'test_' + subtest_name)
    288                 function_to_call()
    289             except AttributeError:
    290                 # Just in case the input test name was mistyped in the control
    291                 #  file.
    292                 raise error.TestFail('No function named: test_' + subtest_name)
    293