tor-browser

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

mediasource-worker-duration.html (4040B)


      1 <!DOCTYPE html>
      2 <html>
      3 <title>Test MediaSource-in-Worker duration updates before and after HAVE_METADATA</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="mediasource-message-util.js"></script>
      7 <body>
      8 <script>
      9 
     10 function awaitDuration(t, video, worker, requestingMessage, expectedDuration) {
     11  let durationAwaiter = t.step_func(() => {
     12    if ((!Number.isNaN(expectedDuration) && video.duration === expectedDuration) ||
     13        (Number.isNaN(expectedDuration) && Number.isNaN(video.duration))) {
     14      worker.postMessage({ subject: messageSubject.ACK_VERIFIED, info: requestingMessage });
     15      return;
     16    }
     17 
     18    // Otherwise, wait for one or more 'durationchange' events to see if video
     19    // eventually has the expectedDuration.
     20    video.addEventListener('durationchange', durationAwaiter, { once: true });
     21  });
     22 
     23  durationAwaiter();
     24 }
     25 
     26 async_test(t => {
     27  // Fail fast if MSE-in-Workers is not supported.
     28  assert_true(MediaSource.hasOwnProperty("canConstructInDedicatedWorker"), "MediaSource hasOwnProperty 'canConstructInDedicatedWorker'");
     29  assert_true(MediaSource.canConstructInDedicatedWorker, "MediaSource.canConstructInDedicatedWorker");
     30 
     31  const video = document.createElement("video");
     32  document.body.appendChild(video);
     33  video.onerror = t.unreached_func("video element error");
     34  video.onended = t.unreached_func("video element ended");
     35  assert_equals(video.duration, NaN, "initial video duration before attachment should be NaN");
     36  assert_equals(video.readyState, HTMLMediaElement.HAVE_NOTHING, "initial video readyState before attachment should be HAVE_NOTHING");
     37 
     38  let worker = new Worker("mediasource-worker-duration.js");
     39  worker.onerror = t.step_func(e => {
     40    assert_unreached("worker error: [" + e.filename + ":" + e.lineno + ":" + e.colno + ":" + e.error + ":" + e.message + "]");
     41  });
     42  worker.onmessage = t.step_func(e => {
     43    let subject = e.data.subject;
     44    assert_true(subject !== undefined, "message must have a subject field");
     45    switch (subject) {
     46      case messageSubject.ERROR:
     47        assert_unreached("Worker error: " + e.data.info);
     48        break;
     49      case messageSubject.HANDLE:
     50        const handle = e.data.info;
     51        assert_equals(video.duration, NaN, "initial video duration before attachment should still be NaN");
     52        assert_equals(video.readyState, HTMLMediaElement.HAVE_NOTHING,
     53                      "initial video readyState before attachment should still be HAVE_NOTHING");
     54        video.srcObject = handle;
     55        break;
     56      case messageSubject.VERIFY_DURATION:
     57        assert_equals(video.duration, e.data.info, "duration should match expectation");
     58        worker.postMessage({ subject: messageSubject.ACK_VERIFIED, info: e.data });
     59        break;
     60      case messageSubject.AWAIT_DURATION:
     61        awaitDuration(t, video, worker, e.data, e.data.info);
     62        break;
     63      case messageSubject.VERIFY_HAVE_NOTHING:
     64        assert_equals(video.readyState, HTMLMediaElement.HAVE_NOTHING, "readyState should match expectation");
     65        worker.postMessage({ subject: messageSubject.ACK_VERIFIED, info: e.data });
     66        break;
     67      case messageSubject.VERIFY_AT_LEAST_HAVE_METADATA:
     68        assert_greater_than_equal(video.readyState, HTMLMediaElement.HAVE_METADATA, "readyState should match expectation");
     69        worker.postMessage({ subject: messageSubject.ACK_VERIFIED, info: e.data });
     70        break;
     71      case messageSubject.WORKER_DONE:
     72        // This test is a worker-driven set of verifications, and it will send
     73        // this message when it is complete. See comment in the worker script
     74        // that describes the phases of this test case.
     75        assert_not_equals(video.srcObject, null, "test should at least have set srcObject.");
     76        t.done();
     77        break;
     78      default:
     79        assert_unreached("Unexpected message subject: " + subject);
     80    }
     81  });
     82 }, "Test worker MediaSource duration updates before and after HAVE_METADATA");
     83 
     84 </script>
     85 </body>
     86 </html>