tor-browser

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

worker_driver.js (1773B)


      1 // Any copyright is dedicated to the Public Domain.
      2 // http://creativecommons.org/publicdomain/zero/1.0/
      3 //
      4 // Utility script for writing worker tests.  In your main document do:
      5 //
      6 //  <script type="text/javascript" src="worker_driver.js"></script>
      7 //  <script type="text/javascript">
      8 //    workerTestExec('myWorkerTestCase.js')
      9 //  </script>
     10 //
     11 // This will then spawn a worker, define some utility functions, and then
     12 // execute the code in myWorkerTestCase.js.  You can then use these
     13 // functions in your worker-side test:
     14 //
     15 //  ok() - like the SimpleTest assert
     16 //  is() - like the SimpleTest assert
     17 //  workerTestDone() - like SimpleTest.finish() indicating the test is complete
     18 //
     19 // There are also some functions for requesting information that requires
     20 // SpecialPowers or other main-thread-only resources:
     21 //
     22 //  workerTestGetVersion() - request the current version string from the MT
     23 //  workerTestGetUserAgent() - request the user agent string from the MT
     24 //  workerTestGetOSCPU() - request the navigator.oscpu string from the MT
     25 //
     26 // For an example see test_worker_interfaces.html and test_worker_interfaces.js.
     27 
     28 function workerTestExec(script) {
     29  SimpleTest.waitForExplicitFinish();
     30  var worker = new Worker("worker_wrapper.js");
     31  worker.onmessage = function (event) {
     32    if (event.data.type == "finish") {
     33      SimpleTest.finish();
     34    } else if (event.data.type == "status") {
     35      ok(event.data.status, event.data.msg);
     36    } else if (event.data.type == "getHelperData") {
     37      worker.postMessage({
     38        type: "returnHelperData",
     39        result: getHelperData(),
     40      });
     41    }
     42  };
     43 
     44  worker.onerror = function (event) {
     45    ok(false, "Worker had an error: " + event.data);
     46    SimpleTest.finish();
     47  };
     48 
     49  worker.postMessage({ script });
     50 }