tor-browser

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

test_WorkerDebugger.xhtml (6055B)


      1 <?xml version="1.0"?>
      2 <!--
      3  Any copyright is dedicated to the Public Domain.
      4  http://creativecommons.org/publicdomain/zero/1.0/
      5 -->
      6 <window title="Test for WorkerDebugger"
      7        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
      8 
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
     10  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
     11  <script type="application/javascript" src="dom_worker_helper.js"/>
     12 
     13  <script type="application/javascript">
     14  <![CDATA[
     15 
     16    const WORKER_URL = "WorkerDebugger_worker.js";
     17    const CHILD_WORKER_URL = "WorkerDebugger_childWorker.js";
     18    const SHARED_WORKER_URL = "WorkerDebugger_sharedWorker.js";
     19 
     20    add_task(
     21      async function runTest() {
     22        info("Create a top-level chrome worker that creates a non-top-level " +
     23             "content worker and wait for their debuggers to be registered.");
     24        let promise = waitForMultiple([
     25          waitForRegister(WORKER_URL),
     26          waitForRegister(CHILD_WORKER_URL)
     27        ]);
     28        worker = new ChromeWorker(WORKER_URL, { name: "worker name" });
     29        let [dbg, childDbg] = await promise;
     30 
     31        info("Check that the top-level chrome worker debugger has the " +
     32             "correct properties.");
     33        is(dbg.isChrome, true,
     34           "Chrome worker debugger should be chrome.");
     35        is(dbg.parent, null,
     36           "Top-level debugger should not have parent.");
     37        is(dbg.type, Ci.nsIWorkerDebugger.TYPE_DEDICATED,
     38           "Chrome worker debugger should be dedicated.");
     39        is(dbg.window, window,
     40           "Top-level dedicated worker debugger should have window.");
     41        is(dbg.name, "worker name",
     42           "Top-level worker name is exposed via name attribute.");
     43 
     44        info("Check that the non-top-level content worker debugger has the " +
     45             "correct properties.");
     46        is(childDbg.isChrome, false,
     47           "Content worker debugger should be content.");
     48        is(childDbg.parent, dbg,
     49           "Non-top-level worker debugger should have parent.");
     50        is(childDbg.type, Ci.nsIWorkerDebugger.TYPE_DEDICATED,
     51           "Content worker debugger should be dedicated.");
     52        is(childDbg.window, window,
     53           "Non-top-level worker debugger should have window.");
     54        is(childDbg.name, "",
     55           "Non-top-level worker doesn't have a custom name");
     56 
     57        info("Terminate the top-level chrome worker and the non-top-level " +
     58             "content worker, and wait for their debuggers to be " +
     59             "unregistered and closed.");
     60        promise = waitForMultiple([
     61          waitForUnregister(CHILD_WORKER_URL),
     62          waitForDebuggerClose(childDbg),
     63          waitForUnregister(WORKER_URL),
     64          waitForDebuggerClose(dbg),
     65        ]);
     66        worker.terminate();
     67        await promise;
     68 
     69        info("Create a shared worker and wait for its debugger to be " +
     70             "registered");
     71        promise = waitForRegister(SHARED_WORKER_URL);
     72        worker = new SharedWorker(SHARED_WORKER_URL);
     73        let sharedDbg = await promise;
     74 
     75        info("Check that the shared worker debugger has the correct " +
     76             "properties.");
     77        is(sharedDbg.isChrome, false,
     78           "Shared worker debugger should be content.");
     79        is(sharedDbg.parent, null,
     80           "Shared worker debugger should not have parent.");
     81        is(sharedDbg.type, Ci.nsIWorkerDebugger.TYPE_SHARED,
     82           "Shared worker debugger should be shared.");
     83        is(sharedDbg.window, null,
     84           "Shared worker debugger should not have window.");
     85 
     86        info("Create a shared worker with the same URL and check that its " +
     87             "debugger is not registered again.");
     88        let listener = {
     89          onRegistered () {
     90            ok(false,
     91               "Shared worker debugger should not be registered again.");
     92          },
     93        };
     94        wdm.addListener(listener);
     95 
     96        let secondWorker = new SharedWorker(SHARED_WORKER_URL);
     97 
     98        info("Send a message to the shared worker to tell it to close " +
     99             "itself, and wait for its debugger to be closed.");
    100        promise = waitForMultiple([
    101          waitForUnregister(SHARED_WORKER_URL),
    102          waitForDebuggerClose(sharedDbg)
    103        ]);
    104        secondWorker.port.start();
    105        secondWorker.port.postMessage("close");
    106        await promise;
    107        worker = null;
    108        secondWorker = null;
    109 
    110        info("Create a SharedWorker again for the infinite loop test.")
    111        promise = waitForRegister(SHARED_WORKER_URL);
    112        // Give it an explicit name so we don't reuse the above SharedWorker.
    113        worker = new SharedWorker(SHARED_WORKER_URL, "loopy");
    114        sharedDbg = await promise;
    115 
    116        info("Send a message to the shared worker to tell it to close " +
    117             "itself, then loop forever, and wait for its debugger to be closed.");
    118        promise = waitForMultiple([
    119          waitForUnregister(SHARED_WORKER_URL),
    120          waitForDebuggerClose(sharedDbg)
    121        ]);
    122 
    123        // When the closing process begins, we schedule a timer to terminate
    124        // the worker in case it's in an infinite loop, which is exactly what
    125        // we do in this test.  The default delay is 30 seconds.  This test
    126        // previously waited 15 seconds for reasons that were poorly justified.
    127        // We now set it to 100ms because we just want to make sure that the
    128        // timeout mechanism to force cancellation from the parent properly
    129        // works (as long as the parent thread isn't blocked).
    130        await SpecialPowers.pushPrefEnv({"set": [[ "dom.worker.canceling.timeoutMilliseconds", 100 ]]});
    131 
    132        worker.port.start();
    133        worker.port.postMessage("close_loop");
    134        await promise;
    135 
    136        wdm.removeListener(listener);
    137      }
    138    );
    139 
    140  ]]>
    141  </script>
    142 
    143  <body xmlns="http://www.w3.org/1999/xhtml">
    144    <p id="display"></p>
    145    <div id="content" style="display:none;"></div>
    146    <pre id="test"></pre>
    147  </body>
    148  <label id="test-result"/>
    149 </window>