tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MediaDevices-enumerateDevices-returned-objects.https.html (2808B)


      1 <!doctype html>
      2 <html>
      3 <head>
      4 <title>enumerateDevices is returning new MediaDeviceInfo objects every time</title>
      5 <script src=/resources/testharness.js></script>
      6 <script src=/resources/testharnessreport.js></script>
      7 <script src=/resources/testdriver.js></script>
      8 <script src=/resources/testdriver-vendor.js></script>
      9 <script src=permission-helper.js></script>
     10 </head>
     11 <body>
     12 <script>
     13 function doTest(callGetUserMedia, testName)
     14 {
     15    promise_test(async () => {
     16        if (callGetUserMedia) {
     17            await setMediaPermission();
     18            await navigator.mediaDevices.getUserMedia({audio : true, video: true});
     19        }
     20 
     21        const deviceList1 =  await navigator.mediaDevices.enumerateDevices();
     22        const deviceList2 =  await navigator.mediaDevices.enumerateDevices();
     23 
     24        assert_equals(deviceList1.length, deviceList2.length);
     25        for (let i = 0; i < deviceList1.length; i++) {
     26            const device1 = deviceList1[i];
     27            const device2 = deviceList2[i];
     28            assert_not_equals(device1, device2);
     29            assert_equals(device1.deviceId, device2.deviceId, "deviceId");
     30            assert_equals(device1.kind, device2.kind, "kind");
     31            if (!callGetUserMedia) {
     32              /* For camera and microphone devices,
     33               if the browsing context did not capture (i.e. getUserMedia() was not called or never resolved successfully),
     34               the MediaDeviceInfo object will contain a valid value for kind
     35               but empty strings for deviceId, label, and groupId. */
     36              assert_equals(device1.deviceId, "", "deviceId is empty before capture");
     37              assert_equals(device1.groupId, "", "groupId is empty before capture");
     38              assert_equals(device1.label, "", "label is empty before capture");
     39              assert_in_array(device1.kind, ["audioinput", "audiooutput", "videoinput"],
     40                              "kind is set to a valid value before capture");
     41            }
     42        }
     43        /* Additionally, at most one device of each kind
     44           will be listed in enumerateDevices() result. */
     45        // FIXME: ensure browsers are tested as if they had multiple devices of at least one kind -
     46        // this probably needs https://w3c.github.io/mediacapture-automation/ support
     47        if (!callGetUserMedia) {
     48            const deviceKinds = deviceList1.map(d => d.kind);
     49            for (let kind of deviceKinds) {
     50              assert_equals(deviceKinds.filter(x => x===kind).length, 1, "At most 1 " + kind + " prior to capture");
     51            }
     52        }
     53    }, testName);
     54 }
     55 
     56 doTest(false, "enumerateDevices exposes mostly empty objects ahead of successful getUserMedia call");
     57 doTest(true, "enumerateDevices exposes expected objects after successful getUserMedia call");
     58 </script>
     59 </body>
     60 </html>