tor-browser

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

browser_resources_several_resources.js (3600B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Check that the resource command is still properly watching for new targets
      8 * after unwatching one resource, if there is still another watched resource.
      9 */
     10 add_task(async function () {
     11  // We will create a main process target list here in order to monitor
     12  // resources from new tabs as they get created.
     13  await pushPref("devtools.browsertoolbox.scope", "everything");
     14 
     15  // Open a test tab
     16  const tab = await addTab("data:text/html,Root Node tests");
     17 
     18  const { client, resourceCommand, targetCommand } =
     19    await initMultiProcessResourceCommand();
     20 
     21  const { CONSOLE_MESSAGE, ROOT_NODE } = resourceCommand.TYPES;
     22 
     23  // We are only interested in console messages as a resource, the ROOT_NODE one
     24  // is here to test the ResourceCommand::unwatchResources API with several resources.
     25  const receivedMessages = [];
     26  const onAvailable = resources => {
     27    for (const resource of resources) {
     28      if (resource.resourceType === CONSOLE_MESSAGE) {
     29        receivedMessages.push(resource);
     30      }
     31    }
     32  };
     33 
     34  info("Call watchResources([CONSOLE_MESSAGE, ROOT_NODE], ...)");
     35  await resourceCommand.watchResources([CONSOLE_MESSAGE, ROOT_NODE], {
     36    onAvailable,
     37  });
     38 
     39  info("Use console.log in the content page");
     40  logInTab(tab, "test from data-url");
     41  info(
     42    "Wait until onAvailable received the CONSOLE_MESSAGE resource emitted from the data-url tab"
     43  );
     44  await waitUntil(() =>
     45    receivedMessages.find(
     46      resource => resource.arguments[0] === "test from data-url"
     47    )
     48  );
     49 
     50  // Check that the resource command captures resources from new targets.
     51  info("Open a first tab on the example.com domain");
     52  const comTab = await addTab(
     53    "https://example.com/document-builder.sjs?html=com"
     54  );
     55  info("Use console.log in the example.com page");
     56  logInTab(comTab, "test-from-example-com");
     57  info(
     58    "Wait until onAvailable received the CONSOLE_MESSAGE resource emitted from the example.com tab"
     59  );
     60  await waitUntil(() =>
     61    receivedMessages.find(
     62      resource => resource.arguments[0] === "test-from-example-com"
     63    )
     64  );
     65 
     66  info("Stop watching ROOT_NODE resources");
     67  await resourceCommand.unwatchResources([ROOT_NODE], { onAvailable });
     68 
     69  // Check that messages from new targets are still captured after calling
     70  // unwatch for another resource.
     71  info("Open a second tab on the example.org domain");
     72  const orgTab = await addTab(
     73    "https://example.org/document-builder.sjs?html=org"
     74  );
     75  info("Use console.log in the example.org page");
     76  logInTab(orgTab, "test-from-example-org");
     77  info(
     78    "Wait until onAvailable received the CONSOLE_MESSAGE resource emitted from the example.org tab"
     79  );
     80  await waitUntil(() =>
     81    receivedMessages.find(
     82      resource => resource.arguments[0] === "test-from-example-org"
     83    )
     84  );
     85 
     86  info("Stop watching CONSOLE_MESSAGE resources");
     87  await resourceCommand.unwatchResources([CONSOLE_MESSAGE], { onAvailable });
     88  await logInTab(tab, "test-again");
     89 
     90  // We don't have a specific event to wait for here, so allow some time for
     91  // the message to be received.
     92  await wait(1000);
     93 
     94  is(
     95    receivedMessages.find(resource => resource.arguments[0] === "test-again"),
     96    undefined,
     97    "The resource command should not watch CONSOLE_MESSAGE anymore"
     98  );
     99 
    100  // Cleanup
    101  targetCommand.destroy();
    102  await client.close();
    103 });
    104 
    105 function logInTab(tab, message) {
    106  return ContentTask.spawn(tab.linkedBrowser, message, function (_message) {
    107    content.console.log(_message);
    108  });
    109 }