playback-temporary-events.js (6226B)
1 function runTest(config,qualifier) { 2 3 var testname = testnamePrefix(qualifier, config.keysystem) 4 + ', temporary, ' 5 + /video\/([^;]*)/.exec(config.videoType)[1] 6 + ', playback, check events'; 7 8 var configuration = { initDataTypes: [ config.initDataType ], 9 audioCapabilities: [ { contentType: config.audioType } ], 10 videoCapabilities: [ { contentType: config.videoType } ], 11 sessionTypes: [ 'temporary' ] }; 12 13 async_test(function(test) { 14 var _video = config.video, 15 _mediaKeys, 16 _mediaKeySession, 17 _mediaSource, 18 _resolvedClosePromises = 0, 19 _timeupdateEvent = false, 20 _events = [ ]; 21 22 function onFailure(error) { 23 forceTestFailureFromPromise(test, error); 24 } 25 26 function onMessage(event) { 27 assert_equals(event.target, _mediaKeySession); 28 assert_true(event instanceof window.MediaKeyMessageEvent); 29 assert_equals(event.type, 'message'); 30 31 assert_in_array( event.messageType, ['license-request', 'individualization-request']); 32 33 if (event.messageType !== 'individualization-request') { 34 _events.push(event.messageType); 35 } 36 37 config.messagehandler(event.messageType, event.message).then(function(response) { 38 _events.push('license-request-response'); 39 waitForEventAndRunStep('keystatuseschange', _mediaKeySession, onKeyStatusesChange, test); 40 return _mediaKeySession.update( response ); 41 }).then(function() { 42 _events.push('update-resolved'); 43 }).catch(onFailure); 44 } 45 46 function onKeyStatusesChange(event) { 47 assert_equals(event.target, _mediaKeySession); 48 assert_true(event instanceof window.Event); 49 assert_equals(event.type, 'keystatuseschange'); 50 var hasKeys = false, pendingKeys = false; 51 _mediaKeySession.keyStatuses.forEach(function(value, keyid) { 52 assert_in_array(value, ['status-pending', 'usable']); 53 hasKeys = true; 54 pendingKeys = pendingKeys || (value === 'status-pending'); 55 }); 56 57 if (!hasKeys) { 58 _events.push('emptykeyslist'); 59 } else if (!pendingKeys ) { 60 _events.push('allkeysusable'); 61 _video.setMediaKeys(_mediaKeys).catch(onFailure); 62 } else { 63 assert_unreached('unexpected ' + event.type + ' event'); 64 } 65 } 66 67 function onEncrypted(event) { 68 assert_equals(event.target, _video); 69 assert_true(event instanceof window.MediaEncryptedEvent); 70 assert_equals(event.type, 'encrypted'); 71 72 waitForEventAndRunStep('message', _mediaKeySession, onMessage, test); 73 _mediaKeySession.generateRequest(config.initData ? config.initDataType : event.initDataType, 74 config.initData || event.initData ).then(function() { 75 _events.push('generaterequest'); 76 }).catch(onFailure); 77 } 78 79 function onAllClosed() { 80 setTimeout(test.step_func(function() { 81 checkEventSequence( _events, 82 ['generaterequest', 83 ['license-request', 'license-request-response', 'update-resolved'], // potentially repeating 84 'allkeysusable', 85 'playing', 86 'closed-promise-0', 87 'closed-promise-1', 88 'emptykeyslist']); 89 test.done(); 90 } ), 0); 91 } 92 93 function onClosed() { 94 // The two closed Promises are equivalent in every way. The order between them does not matter. 95 // But both should be resolved at the right time relative to the other events. 96 // We generate numbered events for them (e.g. 'closed-promise-0') so they can both be placed in 97 // the overall timeline, but we don't care which is which. 98 _events.push('closed-promise-' + _resolvedClosePromises); 99 _resolvedClosePromises++; 100 } 101 102 function onTimeupdate(event) { 103 if (_video.currentTime > (config.duration || 1) && !_timeupdateEvent) { 104 _timeupdateEvent = true; 105 _video.pause(); 106 107 var closedAttributePromise = _mediaKeySession.closed; 108 var closeMethodPromise = _mediaKeySession.close(); 109 110 closedAttributePromise.then(onClosed); 111 closeMethodPromise.then(onClosed); 112 113 Promise.all([ closedAttributePromise, closeMethodPromise ]).then(function() { 114 test.step_func(onAllClosed)(); 115 }).catch(onFailure); 116 } 117 } 118 119 function onPlaying(event) { 120 _events.push('playing'); 121 122 // Not using waitForEventAndRunStep() to avoid too many 123 // EVENT(onTimeUpdate) logs. 124 _video.addEventListener('timeupdate', onTimeupdate, true); 125 } 126 127 navigator.requestMediaKeySystemAccess(config.keysystem, [configuration]).then(function(access) { 128 return access.createMediaKeys(); 129 }).then(function(mediaKeys) { 130 _mediaKeys = mediaKeys; 131 _mediaKeySession = _mediaKeys.createSession('temporary'); 132 133 waitForEventAndRunStep('encrypted', _video, onEncrypted, test); 134 waitForEventAndRunStep('playing', _video, onPlaying, test); 135 }).then(function() { 136 return testmediasource(config); 137 }).then(function(source) { 138 _mediaSource = source; 139 _video.src = URL.createObjectURL(_mediaSource); 140 return source.done; 141 }).then(function(){ 142 _video.play(); 143 }).catch(onFailure); 144 }, testname); 145 }