playback-temporary-expired.js (4429B)
1 function runTest(config,qualifier) { 2 3 var testname = testnamePrefix(qualifier, config.keysystem) 4 + ', temporary, ' 5 + /video\/([^;]*)/.exec(config.videoType)[1] 6 + ', expired license'; 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 15 var _video = config.video, 16 _mediaKeys, 17 _mediaKeySession, 18 _mediaSource; 19 20 function onFailure(error) { 21 forceTestFailureFromPromise(test, error); 22 } 23 24 function onEncrypted(event) { 25 assert_equals(event.target, _video); 26 assert_true(event instanceof window.MediaEncryptedEvent); 27 assert_equals(event.type, 'encrypted'); 28 29 // Only create the session for the first encrypted event 30 if (_mediaKeySession !== undefined) return; 31 32 var initDataType = config.initData ? config.initDataType : event.initDataType; 33 var initData = config.initData || event.initData; 34 35 _mediaKeySession = _mediaKeys.createSession('temporary'); 36 waitForEventAndRunStep('message', _mediaKeySession, onMessage, test); 37 _mediaKeySession.generateRequest(initDataType, initData).catch(onFailure); 38 } 39 40 function onMessage(event) { 41 assert_equals(event.target, _mediaKeySession); 42 assert_true(event instanceof window.MediaKeyMessageEvent); 43 assert_equals(event.type, 'message'); 44 45 assert_in_array(event.messageType, ['license-request', 'individualization-request']); 46 47 // Generate a license that expires 2 seconds from now. 48 var expiration = Date.now().valueOf() + 2000; 49 config.messagehandler(event.messageType, event.message, { expiration: expiration }).then(function(response) { 50 // Wait 4 seconds before calling update() to ensure that the 51 // license has really expired. This is to avoid problems 52 // where the browser starts buffering frames as soon as a 53 // valid license is received. 54 test.step_timeout(function() { 55 event.target.update(response).then(function() { 56 // License server may only have second granularity, so check 57 // that session expiration time is close to the desired value. 58 assert_approx_equals(event.target.expiration, expiration, 3000, 59 "expiration attribute should equal provided expiration time"); 60 assert_greater_than(Date.now().valueOf(), expiration, "Starting play before license expired"); 61 _video.play(); 62 // Wait 4 seconds to ensure that the video does not play. 63 test.step_timeout(function() { test.done(); }, 4000); 64 }).catch(onFailure); 65 }, 4000); 66 }).catch(onFailure); 67 } 68 69 function onPlaying(event) { 70 // Not using waitForEventAndRunStep() to avoid too many 71 // EVENT(onTimeUpdate) logs. 72 _video.addEventListener('timeupdate', test.step_func(onTimeupdate), true); 73 } 74 75 function onTimeupdate(event) { 76 _video.pause(); 77 assert_unreached("Playback should not start with expired license"); 78 } 79 80 navigator.requestMediaKeySystemAccess(config.keysystem, [configuration]).then(function(access) { 81 return access.createMediaKeys(); 82 }).then(function(mediaKeys) { 83 _mediaKeys = mediaKeys; 84 return _video.setMediaKeys(_mediaKeys); 85 }).then(function(){ 86 waitForEventAndRunStep('encrypted', _video, onEncrypted, test); 87 waitForEventAndRunStep('playing', _video, onPlaying, test); 88 return testmediasource(config); 89 }).then(function(source) { 90 _mediaSource = source; 91 _video.src = URL.createObjectURL(_mediaSource); 92 return source.done; 93 }).catch(onFailure); 94 }, testname); 95 }