tor-browser

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

browser_resources_target_destroy.js (3252B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the server ResourceCommand are destroyed when the associated target actors
      7 // are destroyed.
      8 
      9 add_task(async function () {
     10  const tab = await addTab("data:text/html,Test");
     11  const { client, resourceCommand, targetCommand } =
     12    await initResourceCommand(tab);
     13 
     14  // Start watching for console messages. We don't care about messages here, only the
     15  // registration/destroy mechanism, so we make onAvailable a no-op function.
     16  await resourceCommand.watchResources(
     17    [resourceCommand.TYPES.CONSOLE_MESSAGE],
     18    {
     19      onAvailable: () => {},
     20    }
     21  );
     22 
     23  info(
     24    "Spawn a content task in order to be able to manipulate actors and resource watchers directly"
     25  );
     26  const connectionPrefix = targetCommand.watcherFront.actorID.replace(
     27    /watcher\d+$/,
     28    ""
     29  );
     30  await ContentTask.spawn(
     31    tab.linkedBrowser,
     32    [connectionPrefix],
     33    function (_connectionPrefix) {
     34      const { require } = ChromeUtils.importESModule(
     35        "resource://devtools/shared/loader/Loader.sys.mjs"
     36      );
     37      const { TargetActorRegistry } = ChromeUtils.importESModule(
     38        "resource://devtools/server/actors/targets/target-actor-registry.sys.mjs"
     39      );
     40      const {
     41        getResourceWatcher,
     42        TYPES,
     43      } = require("resource://devtools/server/actors/resources/index.js");
     44 
     45      // Retrieve the target actor instance and its watcher for console messages
     46      const targetActor = TargetActorRegistry.getTargetActors(
     47        {
     48          type: "browser-element",
     49          browserId: content.browsingContext.browserId,
     50        },
     51        _connectionPrefix
     52      ).find(actor => actor.isTopLevelTarget);
     53      ok(
     54        targetActor,
     55        "Got the top level target actor from the content process"
     56      );
     57      const watcher = getResourceWatcher(targetActor, TYPES.CONSOLE_MESSAGE);
     58 
     59      // Storing the target actor in the global so we can retrieve it later, even if it
     60      // was destroyed
     61      content._testTargetActor = targetActor;
     62 
     63      is(!!watcher, true, "The console message resource watcher was created");
     64    }
     65  );
     66 
     67  info("Close the client, which will destroy the target");
     68  targetCommand.destroy();
     69  await client.close();
     70 
     71  info(
     72    "Spawn a content task in order to run some assertions on actors and resource watchers directly"
     73  );
     74  await ContentTask.spawn(tab.linkedBrowser, [], function () {
     75    const { require } = ChromeUtils.importESModule(
     76      "resource://devtools/shared/loader/Loader.sys.mjs"
     77    );
     78    const {
     79      getResourceWatcher,
     80      TYPES,
     81    } = require("resource://devtools/server/actors/resources/index.js");
     82 
     83    ok(
     84      content._testTargetActor && !content._testTargetActor.actorID,
     85      "The target was destroyed when the client was closed"
     86    );
     87 
     88    // Retrieve the console message resource watcher
     89    const watcher = getResourceWatcher(
     90      content._testTargetActor,
     91      TYPES.CONSOLE_MESSAGE
     92    );
     93 
     94    is(
     95      !!watcher,
     96      false,
     97      "The console message resource watcher isn't registered anymore after the target was destroyed"
     98    );
     99 
    100    // Cleanup work variable
    101    delete content._testTargetActor;
    102  });
    103 });