tor-browser

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

window-print-mozPrintCallback-async-001.html (2119B)


      1 <!doctype html>
      2 <meta charset="utf-8">
      3 <title>
      4  Test for window.print(), with mozPrintCallback making async call to obj.done
      5 </title>
      6 <script src="/resources/testharness.js"></script>
      7 <script src="/resources/testharnessreport.js"></script>
      8 <canvas id="myCanvas" width="80" height="80"></canvas>
      9 <script>
     10  let t = async_test(
     11    "window.print(), with mozPrintCallback making async call to obj.done"
     12  );
     13 
     14  // mozPrintCallback needs to ensure that obj.done() will be called, as a signal
     15  // that it's done with its rendering work. Here, we make that call via an async
     16  // task, scheduled via setTimeout, to mimic the asynchronous work that's
     17  // involved in real-life mozPrintCallback scenarios (e.g. PDF.js waiting for
     18  // assets like fonts to be ready before it can render the content).
     19  myCanvas.mozPrintCallback = function (obj) {
     20    setTimeout(() => {
     21      obj.done();
     22    }, 0);
     23  };
     24 
     25  // window.print shouldn't block the main thread for any substantial time here.
     26  //
     27  // To validate this expectation, we see how soon after calling window.print()
     28  // we're able to execute another task on the main thread (scheduled via
     29  // requestAnimationFrame), and we expect the delay to be (substantially) less
     30  // than this TOO_LONG_WINDOW_PRINT_DUR threshold. (In bug 1980356, we were
     31  // consistently overshooting this, because window.print would spawn a task that
     32  // would block the main thread until the page print timer "watchdog" kicked in
     33  // after ~10 seconds.)
     34  const TOO_LONG_WINDOW_PRINT_DUR = 5000; // 5 seconds
     35 
     36  window.addEventListener("load", () => {
     37    let timestampBeforePrint = performance.now();
     38    window.print();
     39 
     40    // See how soon we're able to execute another task, scheduled via rAF:
     41    requestAnimationFrame(
     42      t.step_func(function () {
     43        let timestampAfterPrint = performance.now();
     44        let printDur = timestampAfterPrint - timestampBeforePrint;
     45        assert_less_than(
     46          printDur,
     47          TOO_LONG_WINDOW_PRINT_DUR,
     48          "window.print shouldn't block main thread for too long."
     49        );
     50        t.done();
     51      })
     52    );
     53  });
     54 </script>