playback-retrieve-persistent-license.js (5122B)
1 function runTest(config,qualifier) { 2 3 var testname = testnamePrefix(qualifier, config.keysystem) 4 + ', persistent-license, ' 5 + /video\/([^;]*)/.exec(config.videoType)[1] 6 + ', ' + config.testcase; 7 8 var configuration = { initDataTypes: [ config.initDataType ], 9 audioCapabilities: [ { contentType: config.audioType } ], 10 videoCapabilities: [ { contentType: config.videoType } ], 11 sessionTypes: [ 'persistent-license' ] }; 12 13 14 async_test( function(test) { 15 var _video = config.video, 16 _mediaKeys, 17 _mediaKeySession, 18 _mediaSource, 19 _sessionId; 20 21 function onFailure(error) { 22 forceTestFailureFromPromise(test, error); 23 } 24 25 function onEncrypted(event) { 26 assert_equals(event.target, _video); 27 assert_true(event instanceof window.MediaEncryptedEvent); 28 assert_equals(event.type, 'encrypted'); 29 30 waitForEventAndRunStep('message', _mediaKeySession, onMessage, test); 31 _mediaKeySession.generateRequest( config.initData ? config.initDataType : event.initDataType, 32 config.initData || event.initData ).then( function() { 33 _sessionId = _mediaKeySession.sessionId; 34 }).catch(onFailure); 35 } 36 37 function onMessage(event) { 38 assert_equals(event.target, _mediaKeySession); 39 assert_true(event instanceof window.MediaKeyMessageEvent); 40 assert_equals(event.type, 'message'); 41 42 assert_in_array(event.messageType, ['license-request', 'individualization-request']); 43 44 config.messagehandler(event.messageType, event.message).then(function(response) { 45 return _mediaKeySession.update(response); 46 }).catch(onFailure); 47 } 48 49 function onPlaying(event) { 50 // Not using waitForEventAndRunStep() to avoid too many 51 // EVENT(onTimeUpdate) logs. 52 _video.addEventListener('timeupdate', onTimeupdate); 53 } 54 55 function onTimeupdate(event) { 56 if (_video.currentTime > (config.duration || 1)) { 57 _video.removeEventListener('timeupdate', onTimeupdate); 58 _video.pause(); 59 _video.removeAttribute('src'); 60 _video.load(); 61 62 _mediaKeySession.closed 63 .then(test.step_func(onClosed)) 64 .catch(onFailure); 65 _mediaKeySession.close() 66 .catch(onFailure); 67 } 68 } 69 70 function onClosed() { 71 // Open a new window in which we will attempt to play with the persisted license 72 var win = window.open(config.windowscript); 73 assert_not_equals(win, null, "Popup windows not allowed?"); 74 75 // Lisen for an event from the new window containing its test assertions 76 window.addEventListener('message', test.step_func(function(messageEvent) { 77 if (messageEvent.data.testResult) { 78 messageEvent.data.testResult.forEach(test.step_func(function(assertion) { 79 assert_equals(assertion.actual, assertion.expected, assertion.message); 80 })); 81 82 win.close(); 83 test.done(); 84 } 85 })); 86 87 // Delete things which can't be cloned and posted over to the new window 88 delete config.video; 89 delete config.messagehandler; 90 91 // Post the config and session id to the new window when it is ready 92 win.onload = function() { 93 win.postMessage({config: config, sessionId: _sessionId}, '*'); 94 } 95 } 96 97 navigator.requestMediaKeySystemAccess(config.keysystem, [configuration]).then(function(access) { 98 return access.createMediaKeys(); 99 }).then(function(mediaKeys) { 100 _mediaKeys = mediaKeys; 101 return _video.setMediaKeys( mediaKeys ); 102 }).then(function() { 103 _mediaKeySession = _mediaKeys.createSession('persistent-license'); 104 waitForEventAndRunStep('encrypted', _video, onEncrypted, test); 105 waitForEventAndRunStep('playing', _video, onPlaying, test); 106 return testmediasource(config); 107 }).then(function(source) { 108 _mediaSource = source; 109 _video.src = URL.createObjectURL(_mediaSource); 110 return source.done; 111 }).then(function(){ 112 _video.play(); 113 }).catch(function (error) { 114 if (error && error.name === 'NotSupportedError') { 115 // If persistent license is not supported, skip test as 116 // key systems are not required to support this session 117 // type. 118 test.done(); 119 } else { 120 onFailure(error); 121 } 122 }); 123 }, testname); 124 }