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