tor-browser

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

MediaStreamTrack-getCapabilities.https.html (5817B)


      1 <!doctype html>
      2 <title>MediaStreamTrack and InputDeviceInfo GetCapabilities</title>
      3 <script src=/resources/testharness.js></script>
      4 <script src=/resources/testharnessreport.js></script>
      5 <script src=/resources/testdriver.js></script>
      6 <script src=/resources/testdriver-vendor.js></script>
      7 <script src=permission-helper.js></script>
      8 <script>
      9 
     10 const audioProperties = [
     11  {name: "sampleRate", type: "number"},
     12  {name: "sampleSize", type: "number"},
     13  {name: "echoCancellation", type: "boolean or string", validValues: [true, false, "all", "remote-only"]},
     14  {name: "autoGainControl", type: "boolean"},
     15  {name: "noiseSuppression", type: "boolean"},
     16  {name: "voiceIsolation", type: "boolean"},
     17  {name: "latency", type: "number"},
     18  {name: "channelCount", type: "number"},
     19  {name: "deviceId", type: "string"},
     20  {name: "groupId", type: "string"}
     21 ];
     22 
     23 const videoProperties = [
     24  {name: "width", type: "number"},
     25  {name: "height", type: "number"},
     26  {name: "aspectRatio", type: "number"},
     27  {name: "frameRate", type: "number"},
     28  {name: "facingMode", type: "enum-any", validValues: ["user", "environment", "left", "right"]},
     29  {name: "resizeMode", type: "enum-all", validValues: ["none", "crop-and-scale"]},
     30  {name: "deviceId", type: "string"},
     31  {name: "groupId", type: "string"},
     32 ];
     33 
     34 function verifyBooleanCapability(capability) {
     35  assert_less_than_equal(capability.length, 2);
     36  capability.forEach(c => assert_equals(typeof c, "boolean"));
     37 }
     38 
     39 function verifyNumberCapability(capability) {
     40    assert_equals(typeof capability, "object");
     41    assert_equals(Object.keys(capability).length, 2);
     42    assert_true(capability.hasOwnProperty('min'));
     43    assert_true(capability.hasOwnProperty('max'));
     44    assert_less_than_equal(capability.min, capability.max);
     45 }
     46 
     47 // Verify that any value provided by an enum capability is in the set of valid
     48 // values.
     49 function verifyEnumAnyCapability(capability, enumMembers) {
     50  capability.forEach(c => {
     51    assert_equals(typeof c, "string");
     52    assert_in_array(c, enumMembers);
     53  });
     54 }
     55 
     56 // Verify that all required values are supported by a capability.
     57 function verifyEnumAllCapability(capability, enumMembers, testNamePrefix) {
     58  enumMembers.forEach(member => {
     59    test(() => {
     60      assert_in_array(member, capability);
     61    }, testNamePrefix + " Value: " + member);
     62  });
     63 }
     64 
     65 function verifyBooleanOrStringCapability(capability, validMembers) {
     66  capability.forEach(c => {
     67    assert_true(typeof c == "boolean" || typeof c == "string");
     68    assert_in_array(c, validMembers);
     69  });
     70 }
     71 
     72 function testCapabilities(capabilities, property, testNamePrefix) {
     73  let testName = testNamePrefix + " " + property.name;
     74  test(() => {
     75    assert_true(capabilities.hasOwnProperty(property.name));
     76  }, testName + " property present.");
     77 
     78  const capability = capabilities[property.name];
     79  testName += " properly supported.";
     80  if (property.type == "string") {
     81    test(() => {
     82      assert_equals(typeof capability, "string");
     83    }, testName);
     84  }
     85 
     86  if (property.type == "boolean") {
     87    test(() => {
     88      verifyBooleanCapability(capability);
     89    }, testName);
     90  }
     91 
     92  if (property.type == "boolean or string") {
     93    test(() => {
     94      verifyBooleanOrStringCapability(capability, property.validValues);
     95    }, testName);
     96  }
     97 
     98  if (property.type == "number") {
     99    test(() => {
    100      verifyNumberCapability(capability);
    101    }, testName);
    102  }
    103 
    104  if (property.type.startsWith("enum")) {
    105    test(() => {
    106      verifyEnumAnyCapability(capability, property.validValues);
    107    }, testName);
    108 
    109    if (property.type == "enum-all") {
    110      verifyEnumAllCapability(capability, property.validValues, testName);
    111    }
    112  }
    113 }
    114 
    115 {
    116  audioProperties.forEach((property, i) => {
    117    promise_test(async t => {
    118      if (i === 0) await setMediaPermission("granted", ["microphone"]);
    119      const stream = await navigator.mediaDevices.getUserMedia({audio: true});
    120      t.add_cleanup(() => stream.getAudioTracks()[0].stop());
    121      const audioCapabilities = stream.getAudioTracks()[0].getCapabilities();
    122      testCapabilities(audioCapabilities, property, "Audio track getCapabilities()");
    123    }, "Setup audio MediaStreamTrack getCapabilities() test for " + property.name);
    124  });
    125 
    126  videoProperties.forEach(property => {
    127    promise_test(async t => {
    128      const stream = await navigator.mediaDevices.getUserMedia({video: true});
    129      t.add_cleanup(() => stream.getVideoTracks()[0].stop());
    130      const audioCapabilities = stream.getVideoTracks()[0].getCapabilities();
    131      testCapabilities(audioCapabilities, property, "Video track getCapabilities()");
    132    }, "Setup video MediaStreamTrack getCapabilities() test for " + property.name);
    133  });
    134 }
    135 
    136 {
    137  audioProperties.forEach(property => {
    138    promise_test(async t => {
    139      const devices = await navigator.mediaDevices.enumerateDevices();
    140      for (const device of devices) {
    141        // Test only one device.
    142        if (device.kind == "audioinput") {
    143          assert_inherits(device, "getCapabilities");
    144          const capabilities = device.getCapabilities();
    145          testCapabilities(capabilities, property, "Audio device getCapabilities()");
    146          break;
    147        }
    148      }
    149    }, "Setup audio InputDeviceInfo getCapabilities() test for " + property.name);
    150  });
    151 
    152  videoProperties.forEach(property => {
    153    promise_test(async t => {
    154      const devices = await navigator.mediaDevices.enumerateDevices();
    155      for (const device of devices) {
    156        // Test only one device.
    157        if (device.kind == "videoinput") {
    158          assert_inherits(device, "getCapabilities");
    159          const capabilities = device.getCapabilities();
    160          testCapabilities(capabilities, property, "Video device getCapabilities()");
    161          break;
    162        }
    163      }
    164    }, "Setup video InputDeviceInfo getCapabilities() test for " + property.name);
    165  });
    166 }
    167 </script>