summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/inspector-helpers.js
blob: 799a2893ec7cbbe5b81a4b139a59d85f469300c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* exported assertOwnershipTrees, checkMissing, waitForMutation,
   isSrcChange, isUnretained, isChildList */

function serverOwnershipTree(walkerArg) {
  return SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [[walkerArg.actorID]],
    function (actorID) {
      const { require } = ChromeUtils.importESModule(
        "resource://devtools/shared/loader/Loader.sys.mjs"
      );
      const {
        DevToolsServer,
      } = require("resource://devtools/server/devtools-server.js");
      const {
        DocumentWalker,
      } = require("resource://devtools/server/actors/inspector/document-walker.js");

      // Convert actorID to current compartment string otherwise
      // searchAllConnectionsForActor is confused and won't find the actor.
      actorID = String(actorID);
      const serverWalker = DevToolsServer.searchAllConnectionsForActor(actorID);

      function sortOwnershipChildrenContentScript(children) {
        return children.sort((a, b) => a.name.localeCompare(b.name));
      }

      function serverOwnershipSubtree(walker, node) {
        const actor = walker.getNode(node);
        if (!actor) {
          return undefined;
        }

        const children = [];
        const docwalker = new DocumentWalker(node, content);
        let child = docwalker.firstChild();
        while (child) {
          const item = serverOwnershipSubtree(walker, child);
          if (item) {
            children.push(item);
          }
          child = docwalker.nextSibling();
        }
        return {
          name: actor.actorID,
          children: sortOwnershipChildrenContentScript(children),
        };
      }
      return {
        root: serverOwnershipSubtree(serverWalker, serverWalker.rootDoc),
        orphaned: [...serverWalker._orphaned].map(o =>
          serverOwnershipSubtree(serverWalker, o.rawNode)
        ),
        retained: [...serverWalker._retainedOrphans].map(o =>
          serverOwnershipSubtree(serverWalker, o.rawNode)
        ),
      };
    }
  );
}

function sortOwnershipChildren(children) {
  return children.sort((a, b) => a.name.localeCompare(b.name));
}

function clientOwnershipSubtree(node) {
  return {
    name: node.actorID,
    children: sortOwnershipChildren(
      node.treeChildren().map(child => clientOwnershipSubtree(child))
    ),
  };
}

function clientOwnershipTree(walker) {
  return {
    root: clientOwnershipSubtree(walker.rootNode),
    orphaned: [...walker._orphaned].map(o => clientOwnershipSubtree(o)),
    retained: [...walker._retainedOrphans].map(o => clientOwnershipSubtree(o)),
  };
}

function ownershipTreeSize(tree) {
  let size = 1;
  for (const child of tree.children) {
    size += ownershipTreeSize(child);
  }
  return size;
}

async function assertOwnershipTrees(walker) {
  const serverTree = await serverOwnershipTree(walker);
  const clientTree = clientOwnershipTree(walker);
  is(
    JSON.stringify(clientTree, null, " "),
    JSON.stringify(serverTree, null, " "),
    "Server and client ownership trees should match."
  );

  return ownershipTreeSize(clientTree.root);
}

// Verify that an actorID is inaccessible both from the client library and the server.
function checkMissing({ client }, actorID) {
  return new Promise(resolve => {
    const front = client.getFrontByID(actorID);
    ok(
      !front,
      "Front shouldn't be accessible from the client for actorID: " + actorID
    );

    client
      .request(
        {
          to: actorID,
          type: "request",
        },
        response => {
          is(
            response.error,
            "noSuchActor",
            "node list actor should no longer be contactable."
          );
          resolve(undefined);
        }
      )
      .catch(() => {});
  });
}

// Load mutations aren't predictable, so keep accumulating mutations until
// the one we're looking for shows up.
function waitForMutation(walker, test, mutations = []) {
  return new Promise(resolve => {
    for (const change of mutations) {
      if (test(change)) {
        resolve(mutations);
      }
    }

    walker.once("mutations", newMutations => {
      waitForMutation(walker, test, mutations.concat(newMutations)).then(
        finalMutations => {
          resolve(finalMutations);
        }
      );
    });
  });
}

function isSrcChange(change) {
  return change.type === "attributes" && change.attributeName === "src";
}

function isUnretained(change) {
  return change.type === "unretained";
}

function isChildList(change) {
  return change.type === "childList";
}