MediaDevices-enumerateDevices.https.html (5501B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>enumerateDevices: test that enumerateDevices is present</title> 5 <link rel="author" title="Dr Alex Gouaillard" href="mailto:agouaillard@gmail.com"/> 6 <link rel="help" href="https://w3c.github.io/mediacapture-main/#enumerating-devices"> 7 <meta name='assert' content='Check that the enumerateDevices() method is present.'/> 8 </head> 9 <body> 10 <h1 class="instructions">Description</h1> 11 <p class="instructions">This test checks for the presence of the 12 <code>navigator.mediaDevices.enumerateDevices()</code> method.</p> 13 <div id='log'></div> 14 <script src=/resources/testharness.js></script> 15 <script src=/resources/testharnessreport.js></script> 16 <script src=/resources/testdriver.js></script> 17 <script src=/resources/testdriver-vendor.js></script> 18 <script src=permission-helper.js></script> 19 <script> 20 "use strict"; 21 22 //NOTE ALEX: for completion, a test for ondevicechange event is missing. 23 24 promise_test(async () => { 25 assert_not_equals(navigator.mediaDevices.enumerateDevices, undefined, "navigator.mediaDevices.enumerateDevices exists"); 26 const devices = await navigator.mediaDevices.enumerateDevices(); 27 for (const {kind, deviceId, label, groupId} of devices) { 28 assert_in_array(kind, ["videoinput", "audioinput", "audiooutput"]); 29 assert_equals(deviceId, "", "deviceId should be empty string if getUserMedia was never called successfully."); 30 assert_equals(label, "", "label should be empty string if getUserMedia was never called successfully."); 31 assert_equals(groupId, "", "groupId should be empty string if getUserMedia was never called successfully."); 32 } 33 assert_less_than_equal(devices.filter(({kind}) => kind == "audioinput").length, 34 1, "there should be zero or one audio input device."); 35 assert_less_than_equal(devices.filter(({kind}) => kind == "videoinput").length, 36 1, "there should be zero or one video input device."); 37 assert_equals(devices.filter(({kind}) => kind == "audiooutput").length, 38 0, "there should be no audio output devices."); 39 assert_less_than_equal(devices.length, 2, 40 "there should be no more than two devices."); 41 if (devices.length > 1) { 42 assert_equals(devices[0].kind, "audioinput", "audioinput is first"); 43 assert_equals(devices[1].kind, "videoinput", "videoinput is second"); 44 } 45 }, "mediaDevices.enumerateDevices() is present and working - before capture"); 46 47 promise_test(async t => { 48 await setMediaPermission("granted"); 49 const stream = await navigator.mediaDevices.getUserMedia({ video: true }); 50 stream.getTracks()[0].stop(); 51 52 const devices = await navigator.mediaDevices.enumerateDevices(); 53 const kinds = ["audioinput", "videoinput"]; 54 for (const {kind, deviceId} of devices) { 55 assert_in_array(kind, kinds, "camera doesn't expose audiooutput"); 56 assert_equals(typeof deviceId, "string", "deviceId is a string."); 57 switch (kind) { 58 case "videoinput": 59 assert_greater_than(deviceId.length, 0, "video deviceId should not be empty."); 60 break; 61 case "audioinput": 62 assert_equals(deviceId.length, 0, "audio deviceId should be empty."); 63 break; 64 } 65 } 66 }, "mediaDevices.enumerateDevices() is working - after video capture"); 67 68 // This test is designed to come after its video counterpart directly above 69 promise_test(async t => { 70 const devices1 = await navigator.mediaDevices.enumerateDevices(); 71 const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); 72 stream.getTracks()[0].stop(); 73 const devices = await navigator.mediaDevices.enumerateDevices(); 74 assert_equals(devices.filter(({kind}) => kind == "videoinput").length, 75 devices1.filter(({kind}) => kind == "videoinput").length, 76 "same number of (previously exposed) videoinput devices"); 77 assert_greater_than_equal(devices.filter(d => d.kind == "audioinput").length, 78 devices1.filter(d => d.kind == "audioinput").length, 79 "same number or more audioinput devices"); 80 const order = ["audioinput", "videoinput", "audiooutput"]; 81 for (const {kind, deviceId} of devices) { 82 assert_in_array(kind, order, "expected kind"); 83 assert_equals(typeof deviceId, "string", "deviceId is a string."); 84 switch (kind) { 85 case "videoinput": 86 assert_greater_than(deviceId.length, 0, "video deviceId should not be empty."); 87 break; 88 case "audioinput": 89 assert_greater_than(deviceId.length, 0, "audio deviceId should not be empty."); 90 break; 91 } 92 } 93 const kinds = devices.map(({kind}) => kind); 94 const correct = [...kinds].sort((a, b) => order.indexOf(a) - order.indexOf(b)); 95 assert_equals(JSON.stringify(kinds), JSON.stringify(correct), "correct order"); 96 }, "mediaDevices.enumerateDevices() is working - after video then audio capture"); 97 98 promise_test(async () => { 99 const devices = await navigator.mediaDevices.enumerateDevices(); 100 for (const mediaInfo of devices) { 101 if (mediaInfo.kind == "audioinput" || mediaInfo.kind == "videoinput") { 102 assert_true("InputDeviceInfo" in window, "InputDeviceInfo exists"); 103 assert_true(mediaInfo instanceof InputDeviceInfo); 104 } else if (mediaInfo.kind == "audiooutput") { 105 assert_true(mediaInfo instanceof MediaDeviceInfo); 106 } else { 107 assert_unreached("mediaInfo.kind should be one of 'audioinput', 'videoinput', or 'audiooutput'.") 108 } 109 } 110 }, "InputDeviceInfo is supported"); 111 </script> 112 </body> 113 </html>