tor-browser

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

timeOrigin.html (2072B)


      1 <!doctype html>
      2 <html>
      3 <head>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 </head>
      7 <body>
      8 <script>
      9 const windowOrigin = performance.timeOrigin;
     10 
     11 test(() => {
     12  // Use a 30ms cushion when comparing with Date() to account for inaccuracy.
     13  const startTime = Date.now();
     14  assert_greater_than_equal(startTime + 30, windowOrigin, 'Date.now() should be at least as large as the window timeOrigin.');
     15  const startNow = performance.now();
     16  assert_less_than_equal(startTime, windowOrigin + startNow + 30, 'Date.now() should be close to window timeOrigin.');
     17 }, 'Window timeOrigin is close to Date.now() when there is no system clock adjustment.');
     18 
     19 const workerScript = 'postMessage({timeOrigin: performance.timeOrigin})';
     20 const blob = new Blob([workerScript]);
     21 
     22 async_test(function(t) {
     23  const beforeWorkerCreation = performance.now();
     24  const worker = new Worker(URL.createObjectURL(blob));
     25  worker.addEventListener('message', t.step_func_done(function(event) {
     26    const workerOrigin = event.data.timeOrigin;
     27    assert_greater_than_equal(workerOrigin, windowOrigin + beforeWorkerCreation, 'Worker timeOrigin should be greater than the window timeOrigin.');
     28    const afterWorkerCreation = performance.now();
     29    assert_less_than_equal(workerOrigin - windowOrigin, afterWorkerCreation, 'Window and worker timeOrigins should be close.');
     30  }));
     31 }, 'Window and worker timeOrigins are close when created one after another.');
     32 
     33 async_test(function(t) {
     34  this.step_timeout(function() {
     35    const workerCreation = performance.now();
     36    const worker = new Worker(URL.createObjectURL(blob));
     37    worker.addEventListener('message', t.step_func_done(function(event) {
     38      const workerOrigin = event.data.timeOrigin;
     39      assert_greater_than_equal(workerOrigin - windowOrigin, 200, 'We waited 200ms to spawn the second worker, so its timeOrigin should be greater than that of the window.');
     40    }));
     41  }, 200);
     42 }, 'Window and worker timeOrigins differ when worker is created after a delay.');
     43 </script>
     44 </body>
     45 </html>