tor-browser

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

test_mediarecorder_glean.html (4797B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <meta charset="utf-8" />
      5  <title>Glean Test for MediaRecorder</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
      8  <script src="/tests/SimpleTest/GleanTest.js"></script>
      9 </head>
     10 <body>
     11 <pre id="test"></pre>
     12 <script class="testbody" type="text/javascript">
     13 const testConfigs = [
     14  {
     15    type: "video",
     16    containers: [
     17      { name: "mp4" },
     18      { name: "webm" },
     19      { name: "x-matroska", label: "mkv" },
     20    ],
     21    codecs: [
     22      { name: "vp8" },
     23      { name: "vp9" },
     24      { name: "av01.0.19M.08", label: "av1" },
     25      { name: "avc1.64003E", label: "h264" },
     26      { name: "hvc1.1.6.L186.B0", label: "h265" },
     27      { name: "" },
     28      { name: "blah" }, // invalid codec
     29    ],
     30  },
     31  {
     32    type: "audio",
     33    containers: [{ name: "mp4" }, { name: "webm" }, { name: "ogg" }],
     34    codecs: [
     35      { name: "mp4a.40.2", label: "aac" },
     36      { name: "flac" },
     37      { name: "opus" },
     38      { name: "vorbis" },
     39      { name: "" },
     40      { name: "blah" }, // invalid codec
     41      { name: "av01.0.19M.08", label: "av1" }, // invalid codec
     42    ],
     43  },
     44 ];
     45 
     46 const audioLabels = {
     47  mp4: ["aac", "flac", "opus", "unspecified"],
     48  webm: ["opus", "vorbis", "unspecified"],
     49  mkv: ["aac", "flac", "opus", "pcm", "vorbis", "unspecified"],
     50  ogg: ["flac", "opus", "vorbis", "unspecified"],
     51 };
     52 
     53 const validContainerCodecLabels = {
     54  audio: audioLabels,
     55  video: {
     56    mp4: [...audioLabels.mp4, "av1", "h264", "h265", "vp9"],
     57    webm: [...audioLabels.webm, "vp8", "vp9", "av1"],
     58    mkv: [...audioLabels.mkv, "av1", "h264", "h265", "vp8", "vp9"],
     59    ogg: [...audioLabels.ogg, "vp8", "vp9"],
     60  },
     61 };
     62 
     63 add_task(async function testGleanIsTypeSupportedLabels() {
     64  await GleanTest.testResetFOG();
     65 
     66  function getLabel(type, container, codec, emptyCodecReplacement) {
     67    const containerLabel = container.label || container.name;
     68    const codecLabel =
     69      codec.name == "" && emptyCodecReplacement
     70        ? emptyCodecReplacement
     71        : codec.label || codec.name;
     72    const isValid = validContainerCodecLabels[type][containerLabel]?.includes(codecLabel);
     73    return `${containerLabel}_${isValid ? codecLabel : "others"}`;
     74  }
     75 
     76  const singleCodecTests = testConfigs.flatMap(({ type, containers, codecs }) =>
     77    containers.flatMap(container =>
     78      codecs.map(codec => ({
     79        mimeType: `${type}/${container.name};codecs=${codec.name}`,
     80        labels: [getLabel(type, container, codec, "unspecified")], // empty codecs string is valid.
     81      }))
     82    )
     83  );
     84 
     85  const videoConfig = testConfigs.find(c => c.type === "video");
     86  const audioConfig = testConfigs.find(c => c.type === "audio");
     87  const pairedCodecTests = videoConfig.containers.flatMap(container =>
     88    videoConfig.codecs.flatMap(videoCodec =>
     89      audioConfig.codecs.map(audioCodec => ({
     90        mimeType: `video/${container.name};codecs=${videoCodec.name},${audioCodec.name}`,
     91        labels: [
     92          getLabel("video", container, videoCodec),
     93          getLabel("video", container, audioCodec),
     94        ],
     95      }))
     96    )
     97  );
     98 
     99  const allTests = [...singleCodecTests, ...pairedCodecTests];
    100  const counters = {};
    101  for (const { mimeType, labels } of allTests) {
    102    dump(`Testing MediaRecorder.isTypeSupported with mimeType: ${mimeType}, labels: [${labels.join(", ")}]\n`);
    103    MediaRecorder.isTypeSupported(mimeType);
    104    // A single call can update multiple counters, so we check each expected label.
    105    const LabeledCounters = {};
    106    for (const label of labels) {
    107      LabeledCounters[label] = (LabeledCounters[label] || 0) + 1;
    108    }
    109    dump(`  Expected increments: ${JSON.stringify(LabeledCounters)}\n`);
    110    for (const label of Object.keys(LabeledCounters)) {
    111      counters[label] = (counters[label] || 0) + LabeledCounters[label];
    112      const value = await GleanTest.mediaRecorder.mimeTypeQuery[label].testGetValue();
    113      is(value, counters[label], `count for label '${label}' from mimeType '${mimeType}' should be ${counters[label]}`);
    114    }
    115  }
    116 
    117  MediaRecorder.isTypeSupported("");
    118  const emptyValue = await GleanTest.mediaRecorder.mimeTypeQuery.empty.testGetValue();
    119  is(emptyValue, 1, `count for empty mime type in MediaRecorder.isTypeSupported should be 1`);
    120 
    121  MediaRecorder.isTypeSupported("blah");
    122  const othersValue = await GleanTest.mediaRecorder.mimeTypeQuery.others.testGetValue();
    123  is(othersValue, 1, `count for invalid mime type in MediaRecorder.isTypeSupported should be 1`);
    124 
    125  // testGetValue returns value of the stored metric, or null if there is no value.
    126  const unusedValue = await GleanTest.mediaRecorder.mimeTypeQuery.mkv_pcm.testGetValue();
    127  is(unusedValue, null, `count for unused mime type in MediaRecorder.isTypeSupported should be null`);
    128 });
    129 </script>
    130 </body>
    131 </html>