1 # Copyright 2015 The Chromium OS 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 6 import os 7 8 from autotest_lib.client.bin import test 9 from autotest_lib.client.bin import utils 10 from autotest_lib.client.common_lib import error 11 from autotest_lib.client.common_lib.cros import chrome 12 from autotest_lib.client.cros.video import helper_logger 13 14 EXTRA_BROWSER_ARGS = ['--use-fake-ui-for-media-stream', 15 '--use-fake-device-for-media-stream', 16 '--enable-experimental-web-platform-features'] 17 18 # Statistics from the loopback.html page. 19 TEST_PROGRESS = 'testProgress' 20 21 # Polling timeout 22 TIMEOUT = 90 23 24 25 class video_WebRtcMediaRecorder(test.test): 26 """WebRTC Media Recorder test.""" 27 version = 1 28 29 30 def launch_recorder_test(self, test_name): 31 """Launch a recorder test. 32 33 @param test_name: Name of test to run. 34 """ 35 with chrome.Chrome(extra_browser_args=EXTRA_BROWSER_ARGS +\ 36 [helper_logger.chrome_vmodule_flag()], 37 init_network_controller=True) as cr: 38 cr.browser.platform.SetHTTPServerDirectories(self.bindir) 39 self.tab = cr.browser.tabs[0] 40 self.tab.Navigate(cr.browser.platform.http_server.UrlOf( 41 os.path.join(self.bindir, 'loopback_mediarecorder.html'))) 42 self.tab.WaitForDocumentReadyStateToBeComplete() 43 self.tab.EvaluateJavaScript(test_name + "();") 44 if not self.is_test_completed(): 45 logging.error('%s did not complete', test_name) 46 raise error.TestFail('Failed %s' %(test_name)) 47 try: 48 result = self.tab.EvaluateJavaScript('result;') 49 except: 50 logging.error('Cannot retrieve results from javascript') 51 raise error.TestFail('Failed %s' %(test_name)) 52 if result != 'PASS': 53 raise error.TestFail('Failed %s, got %s' %(test_name, 54 result)) 55 56 57 def is_test_completed(self): 58 """Checks if WebRTC MediaRecorder test is done. 59 60 @returns True if test complete, False otherwise. 61 62 """ 63 def test_done(): 64 """Check the testProgress variable in HTML page.""" 65 66 # Wait for test completion on web page. 67 test_progress = self.tab.EvaluateJavaScript(TEST_PROGRESS) 68 return test_progress == 1 69 70 try: 71 utils.poll_for_condition( 72 test_done, timeout=TIMEOUT, 73 exception=error.TestError('Cannot find testProgress.'), 74 sleep_interval=1) 75 except error.TestError: 76 return False 77 else: 78 return True 79 80 81 @helper_logger.video_log_wrapper 82 def run_once(self): 83 """Runs the video_WebRtcMediaRecorder test.""" 84 self.launch_recorder_test('testStartAndRecorderState') 85 self.launch_recorder_test('testStartStopAndRecorderState') 86 self.launch_recorder_test('testStartAndDataAvailable') 87 self.launch_recorder_test('testStartWithTimeSlice') 88 self.launch_recorder_test('testResumeAndRecorderState') 89 self.launch_recorder_test('testIllegalResumeThrowsDOMError') 90 self.launch_recorder_test('testResumeAndDataAvailable') 91 self.launch_recorder_test('testPauseAndRecorderState') 92 self.launch_recorder_test('testPauseStopAndRecorderState') 93 self.launch_recorder_test( 94 'testPausePreventsDataavailableFromBeingFired') 95 self.launch_recorder_test('testIllegalPauseThrowsDOMError') 96 self.launch_recorder_test('testIllegalStopThrowsDOMError') 97 self.launch_recorder_test( 98 'testIllegalStartInRecordingStateThrowsDOMError') 99 self.launch_recorder_test( 100 'testIllegalStartInPausedStateThrowsDOMError') 101 self.launch_recorder_test('testTwoChannelAudio') 102 self.launch_recorder_test('testIllegalRequestDataThrowsDOMError') 103 self.launch_recorder_test('testAddingTrackToMediaStreamFiresErrorEvent') 104 self.launch_recorder_test( 105 'testRemovingTrackFromMediaStreamFiresErrorEvent') 106