tor-browser

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

browser_resources_root_node.js (3913B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the ResourceCommand API around ROOT_NODE
      7 
      8 /**
      9 * The original test still asserts some scenarios using several watchRootNode
     10 * call sites, which is not something we intend to support at the moment in the
     11 * resource command.
     12 *
     13 * Otherwise this test checks the basic behavior of the resource when reloading
     14 * an empty page.
     15 */
     16 add_task(async function () {
     17  // Open a test tab
     18  const tab = await addTab("data:text/html,Root Node tests");
     19 
     20  const { client, resourceCommand, targetCommand } =
     21    await initResourceCommand(tab);
     22 
     23  const browser = gBrowser.selectedBrowser;
     24 
     25  info("Call watchResources([ROOT_NODE], ...)");
     26  let onAvailableCounter = 0;
     27  const onAvailable = resources => (onAvailableCounter += resources.length);
     28  await resourceCommand.watchResources([resourceCommand.TYPES.ROOT_NODE], {
     29    onAvailable,
     30  });
     31 
     32  info("Wait until onAvailable has been called");
     33  await waitUntil(() => onAvailableCounter === 1);
     34  is(onAvailableCounter, 1, "onAvailable has been called 1 time");
     35 
     36  info("Reload the selected browser");
     37  browser.reload();
     38 
     39  info(
     40    "Wait until the watchResources([ROOT_NODE], ...) callback has been called"
     41  );
     42  await waitUntil(() => onAvailableCounter === 2);
     43 
     44  is(onAvailableCounter, 2, "onAvailable has been called 2 times");
     45 
     46  info("Call unwatchResources([ROOT_NODE], ...) for the onAvailable callback");
     47  resourceCommand.unwatchResources([resourceCommand.TYPES.ROOT_NODE], {
     48    onAvailable,
     49  });
     50 
     51  info("Reload the selected browser");
     52  const reloaded = BrowserTestUtils.browserLoaded(browser);
     53  browser.reload();
     54  await reloaded;
     55 
     56  is(
     57    onAvailableCounter,
     58    2,
     59    "onAvailable was not called after calling unwatchResources"
     60  );
     61 
     62  // Cleanup
     63  targetCommand.destroy();
     64  await client.close();
     65 });
     66 
     67 /**
     68 * Test that the watchRootNode API provides the expected node fronts.
     69 */
     70 add_task(async function testRootNodeFrontIsCorrect() {
     71  const tab = await addTab("data:text/html,<div id=div1>");
     72 
     73  const { client, resourceCommand, targetCommand } =
     74    await initResourceCommand(tab);
     75  const browser = gBrowser.selectedBrowser;
     76 
     77  info("Call watchResources([ROOT_NODE], ...)");
     78 
     79  let rootNodeResolve;
     80  let rootNodePromise = new Promise(r => (rootNodeResolve = r));
     81  const onAvailable = ([rootNodeFront]) => rootNodeResolve(rootNodeFront);
     82  await resourceCommand.watchResources([resourceCommand.TYPES.ROOT_NODE], {
     83    onAvailable,
     84  });
     85 
     86  info("Wait until onAvailable has been called");
     87  const root1 = await rootNodePromise;
     88  ok(!!root1, "onAvailable has been called with a valid argument");
     89  is(
     90    root1.resourceType,
     91    resourceCommand.TYPES.ROOT_NODE,
     92    "The resource has the expected type"
     93  );
     94 
     95  info("Check we can query an expected node under the retrieved root");
     96  const div1 = await root1.walkerFront.querySelector(root1, "div");
     97  is(div1.getAttribute("id"), "div1", "Correct root node retrieved");
     98 
     99  info("Reload the selected browser");
    100  rootNodePromise = new Promise(r => (rootNodeResolve = r));
    101  browser.reload();
    102 
    103  const root2 = await rootNodePromise;
    104  Assert.notStrictEqual(
    105    root1,
    106    root2,
    107    "onAvailable has been called with a different node front after reload"
    108  );
    109 
    110  info("Navigate to another URL");
    111  rootNodePromise = new Promise(r => (rootNodeResolve = r));
    112  BrowserTestUtils.startLoadingURIString(
    113    browser,
    114    `data:text/html,<div id=div3>`
    115  );
    116  const root3 = await rootNodePromise;
    117  info("Check we can query an expected node under the retrieved root");
    118  const div3 = await root3.walkerFront.querySelector(root3, "div");
    119  is(div3.getAttribute("id"), "div3", "Correct root node retrieved");
    120 
    121  // Cleanup
    122  resourceCommand.unwatchResources([resourceCommand.TYPES.ROOT_NODE], {
    123    onAvailable,
    124  });
    125  targetCommand.destroy();
    126  await client.close();
    127 });