tor-browser

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

browser_inspector-insert.js (4946B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function () {
      7  const { walker } = await initInspectorFront(
      8    MAIN_DOMAIN + "inspector-traversal-data.html"
      9  );
     10 
     11  await testRearrange(walker);
     12  await testInsertInvalidInput(walker);
     13 });
     14 
     15 async function testRearrange(walker) {
     16  const longlist = await walker.querySelector(walker.rootNode, "#longlist");
     17  let children = await walker.children(longlist);
     18  const nodeA = children.nodes[0];
     19  is(nodeA.id, "a", "Got the expected node.");
     20 
     21  // Move nodeA to the end of the list.
     22  await walker.insertBefore(nodeA, longlist, null);
     23 
     24  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     25    ok(
     26      !content.document.querySelector("#a").nextSibling,
     27      "a should now be at the end of the list."
     28    );
     29  });
     30 
     31  children = await walker.children(longlist);
     32  is(
     33    nodeA,
     34    children.nodes[children.nodes.length - 1],
     35    "a should now be the last returned child."
     36  );
     37 
     38  // Now move it to the middle of the list.
     39  const nextNode = children.nodes[13];
     40  await walker.insertBefore(nodeA, longlist, nextNode);
     41 
     42  await SpecialPowers.spawn(
     43    gBrowser.selectedBrowser,
     44    [[nextNode.actorID]],
     45    async function (actorID) {
     46      const { require } = ChromeUtils.importESModule(
     47        "resource://devtools/shared/loader/Loader.sys.mjs"
     48      );
     49      const {
     50        DevToolsServer,
     51      } = require("resource://devtools/server/devtools-server.js");
     52      const {
     53        DocumentWalker,
     54      } = require("resource://devtools/server/actors/inspector/document-walker.js");
     55      const sibling = new DocumentWalker(
     56        content.document.querySelector("#a"),
     57        content
     58      ).nextSibling();
     59      // Convert actorID to current compartment string otherwise
     60      // searchAllConnectionsForActor is confused and won't find the actor.
     61      actorID = String(actorID);
     62      const nodeActor = DevToolsServer.searchAllConnectionsForActor(actorID);
     63      is(
     64        sibling,
     65        nodeActor.rawNode,
     66        "Node should match the expected next node."
     67      );
     68    }
     69  );
     70 
     71  children = await walker.children(longlist);
     72  is(nodeA, children.nodes[13], "a should be where we expect it.");
     73  is(nextNode, children.nodes[14], "next node should be where we expect it.");
     74 }
     75 
     76 async function testInsertInvalidInput(walker) {
     77  const longlist = await walker.querySelector(walker.rootNode, "#longlist");
     78  const children = await walker.children(longlist);
     79  const nodeA = children.nodes[0];
     80  const nextSibling = children.nodes[1];
     81 
     82  // Now move it to the original location and make sure no mutation happens.
     83  await SpecialPowers.spawn(
     84    gBrowser.selectedBrowser,
     85    [[longlist.actorID]],
     86    async function (actorID) {
     87      const { require } = ChromeUtils.importESModule(
     88        "resource://devtools/shared/loader/Loader.sys.mjs"
     89      );
     90      const {
     91        DevToolsServer,
     92      } = require("resource://devtools/server/devtools-server.js");
     93      // Convert actorID to current compartment string otherwise
     94      // searchAllConnectionsForActor is confused and won't find the actor.
     95      actorID = String(actorID);
     96      const nodeActor = DevToolsServer.searchAllConnectionsForActor(actorID);
     97      content.hasMutated = false;
     98      content.observer = new content.MutationObserver(() => {
     99        content.hasMutated = true;
    100      });
    101      content.observer.observe(nodeActor.rawNode, {
    102        childList: true,
    103      });
    104    }
    105  );
    106 
    107  await walker.insertBefore(nodeA, longlist, nodeA);
    108  let hasMutated = await SpecialPowers.spawn(
    109    gBrowser.selectedBrowser,
    110    [],
    111    async function () {
    112      const state = content.hasMutated;
    113      content.hasMutated = false;
    114      return state;
    115    }
    116  );
    117  ok(!hasMutated, "hasn't mutated");
    118 
    119  await walker.insertBefore(nodeA, longlist, nextSibling);
    120  hasMutated = await SpecialPowers.spawn(
    121    gBrowser.selectedBrowser,
    122    [],
    123    async function () {
    124      const state = content.hasMutated;
    125      content.hasMutated = false;
    126      return state;
    127    }
    128  );
    129  ok(!hasMutated, "still hasn't mutated after inserting before nextSibling");
    130 
    131  await walker.insertBefore(nodeA, longlist);
    132  hasMutated = await SpecialPowers.spawn(
    133    gBrowser.selectedBrowser,
    134    [],
    135    async function () {
    136      const state = content.hasMutated;
    137      content.hasMutated = false;
    138      return state;
    139    }
    140  );
    141  ok(hasMutated, "has mutated after inserting with null sibling");
    142 
    143  await walker.insertBefore(nodeA, longlist);
    144  hasMutated = await SpecialPowers.spawn(
    145    gBrowser.selectedBrowser,
    146    [],
    147    async function () {
    148      const state = content.hasMutated;
    149      content.hasMutated = false;
    150      return state;
    151    }
    152  );
    153  ok(!hasMutated, "hasn't mutated after inserting with null sibling again");
    154 
    155  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    156    content.observer.disconnect();
    157  });
    158 }