tor-browser

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

test_periodic_timeupdate.html (2994B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4 <title>Periodic timeupdate test</title>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      7 <script type="application/javascript">
      8 
      9 /**
     10 * To ensure that when dispatching periodic `timeupdate` event, we would only
     11 * dispatch that once within 250ms according to the spec. This test would end
     12 * after receiving certain number of timeupdate` event.
     13 */
     14 const sNumPeriodicTimeupdatesToEndTestAfter = 5;
     15 const sTimeThreshold = 250;
     16 
     17 add_task(async function testPeriodicTimeupdateShouldOnlyBeDispatchedOnceWithin250Ms() {
     18  const video = document.createElement('video');
     19  video.src = "gizmo.mp4";
     20  video.loop = true;
     21  video._timeupdateCount = 0;
     22  document.body.appendChild(video);
     23  ok(await video.play().then(_=>true,_=>false), "video started playing");
     24  const culprit = createCulpritToMakeMainThreadBusy();
     25  await new Promise(r => {
     26    function endTest() {
     27      video.removeEventListener("timeupdate", checkTimeupdate);
     28      culprit.shutdown();
     29      r();
     30    }
     31    video.onseeking = () => {
     32      info(`seeking starts (looping back to the head)`);
     33      video._ignoreEvents = true;
     34    }
     35    video.onseeked = () => {
     36      info(`seeking ends`);
     37      video._ignoreEvents = false;
     38    }
     39    function checkTimeupdate() {
     40      // When reaching to the end, video would perform a seek to the start
     41      // position where one mandatory `timeupdate` would be dispatched.
     42      if (video._ignoreEvents) {
     43        info(`ignore non-periodic timeupdate because that is allowed to be dispatched within ${sTimeThreshold}ms`);
     44        return;
     45      }
     46 
     47      const now = performance.now();
     48      if (video._prevTime === undefined) {
     49        info(`recevied the first 'timeupdate'`);
     50        video._prevTime = now;
     51        return;
     52      }
     53 
     54      const timeDiff = now - video._prevTime;
     55      if (timeDiff < sTimeThreshold) {
     56        ok(false, `Time diff ${timeDiff} is smaller than ${sTimeThreshold}ms!`);
     57        endTest();
     58        return;
     59      }
     60 
     61      ok(true, `Time diff ${timeDiff} since last time received 'timeupdate'`);
     62      video._prevTime = now;
     63      info(`check timeupdate ${++video._timeupdateCount} time`);
     64      if (video._timeupdateCount == sNumPeriodicTimeupdatesToEndTestAfter) {
     65        endTest();
     66      }
     67    };
     68    video.addEventListener("timeupdate", checkTimeupdate);
     69  });
     70 });
     71 
     72 window.onmessage = _ => blockMainThreadForMilliseconds(1);
     73 
     74 /**
     75 * Following are helper functions
     76 */
     77 function blockMainThreadForMilliseconds(ms) {
     78  const lastTime = performance.now();
     79  while (lastTime + ms > performance.now());
     80 }
     81 
     82 function createCulpritToMakeMainThreadBusy() {
     83  let culprit = {};
     84  culprit._id = setInterval(_ => {
     85    blockMainThreadForMilliseconds(1000);
     86  }, 0);
     87  culprit.shutdown = _ => {
     88    clearInterval(culprit._id);
     89  }
     90  for (let i = 0; i < 5000; ++i) {
     91    window.postMessage("foo", "*");
     92  }
     93  return culprit;
     94 }
     95 
     96 </script>
     97 </head>
     98 <body>
     99 </body>
    100 </html>