ImageCapture-creation.https.html (3457B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src=/resources/testdriver.js></script> 5 <script src=/resources/testdriver-vendor.js></script> 6 <script src='../mediacapture-streams/permission-helper.js'></script> 7 <script> 8 9 // This test verifies that ImageCapture can be created (or not) with different 10 // Media Stream Track types (audio, video). 11 12 function makeAsyncTest(modifyTrack, message) { 13 async_test(function(test) { 14 15 const gotStream = test.step_func(function(stream) { 16 assert_equals(stream.getAudioTracks().length, 0); 17 assert_equals(stream.getVideoTracks().length, 1); 18 19 var videoTrack = stream.getVideoTracks()[0]; 20 assert_equals(videoTrack.readyState, 'live'); 21 assert_true(videoTrack.enabled); 22 assert_false(videoTrack.muted); 23 24 var capturer = new ImageCapture(videoTrack); 25 assert_equals(capturer.track, videoTrack); 26 27 modifyTrack(videoTrack); 28 29 promise_rejects_dom(test, 30 'InvalidStateError', 31 capturer.grabFrame(), 32 'Should throw InvalidStateError.') 33 .then(() => test.done()); 34 }); 35 36 const onError = test.unreached_func('Error creating MediaStream.'); 37 // both permissions are needed at some point, asking for both at once 38 setMediaPermission() 39 .then(() => navigator.mediaDevices.getUserMedia({video: true})) 40 .then(gotStream) 41 .catch(onError); 42 43 }, message); 44 } 45 46 var disableTrack = function(videoTrack) { 47 // grabFrame() is rejected if the associated video track is disabled. 48 videoTrack.enabled = false; 49 }; 50 51 var stopTrack = function(videoTrack) { 52 // grabFrame() is rejected if the associated video track is ended. 53 videoTrack.stop(); 54 assert_equals(videoTrack.readyState, 'ended'); 55 }; 56 57 // Create the rejection tests. Note that grabFrame() would also be rejected if 58 // the video Track was muted but that's a read-only property of the Track. 59 makeAsyncTest(disableTrack, 'grabFrame() of a disabled Track'); 60 makeAsyncTest(stopTrack, 'grabFrame() of an ended Track'); 61 62 63 var testAudio = async_test(function() { 64 navigator.mediaDevices.getUserMedia({audio:true}) 65 .then( 66 this.step_func(function(stream) { 67 assert_equals(stream.getAudioTracks().length, 1); 68 assert_equals(stream.getVideoTracks().length, 0); 69 assert_throws_dom("NotSupportedError", 70 function() { 71 var capturer = new ImageCapture(stream.getAudioTracks()[0]); 72 }, 73 'an ImageCapturer can only be created from a video track'); 74 75 this.done(); 76 })) 77 .catch( 78 this.unreached_func('Error creating MediaStream.')); 79 }, 'verifies that an ImageCapture cannot be created out of an Audio Track'); 80 81 var testParameter = test(function() { 82 const invalidParameters = [ 83 "invalid", 84 null, 85 123, 86 {}, 87 "", 88 true 89 ]; 90 assert_throws_js(TypeError, 91 function() { var capturer = new ImageCapture(); }, 92 'an ImageCapturer cannot be created with no parameter'); 93 invalidParameters.map(parameter => { 94 assert_throws_js(TypeError, 95 function() { var capturer = new ImageCapture(parameter); }, 96 `an ImageCapturer cannot be created with a ${parameter} parameter`); 97 }); 98 }, 'throw "TypeError" if parameter is not MediaStreamTrack.'); 99 100 </script>