summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/head.js
blob: d05c2e0d3deb0ec844907072463679f1678c0544 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* eslint no-unused-vars: [2, {"vars": "local"}] */

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js",
  this
);

const {
  DevToolsClient,
} = require("resource://devtools/client/devtools-client.js");
const {
  DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");

async function _initResourceCommandFromCommands(
  commands,
  { listenForWorkers = false } = {}
) {
  const targetCommand = commands.targetCommand;
  if (listenForWorkers) {
    targetCommand.listenForWorkers = true;
  }
  await targetCommand.startListening();

  //Bug 1709065: Stop exporting resourceCommand and use commands.resourceCommand
  //And rename all these methods
  return {
    client: commands.client,
    commands,
    resourceCommand: commands.resourceCommand,
    targetCommand,
  };
}

/**
 * Instantiate a ResourceCommand for the given tab.
 *
 * @param {Tab} tab
 *        The browser frontend's tab to connect to.
 * @param {Object} options
 * @param {Boolean} options.listenForWorkers
 * @return {Object} object
 * @return {ResourceCommand} object.resourceCommand
 *         The underlying resource command interface.
 * @return {Object} object.commands
 *         The commands object defined by modules from devtools/shared/commands.
 * @return {DevToolsClient} object.client
 *         The underlying client instance.
 * @return {TargetCommand} object.targetCommand
 *         The underlying target list instance.
 */
async function initResourceCommand(tab, options) {
  const commands = await CommandsFactory.forTab(tab);
  return _initResourceCommandFromCommands(commands, options);
}

/**
 * Instantiate a multi-process ResourceCommand, watching all type of targets.
 *
 * @return {Object} object
 * @return {ResourceCommand} object.resourceCommand
 *         The underlying resource command interface.
 * @return {Object} object.commands
 *         The commands object defined by modules from devtools/shared/commands.
 * @return {DevToolsClient} object.client
 *         The underlying client instance.
 * @return {DevToolsClient} object.targetCommand
 *         The underlying target list instance.
 */
async function initMultiProcessResourceCommand() {
  const commands = await CommandsFactory.forMainProcess();
  return _initResourceCommandFromCommands(commands);
}

// Copied from devtools/shared/webconsole/test/chrome/common.js
function checkObject(object, expected) {
  if (object && object.getGrip) {
    object = object.getGrip();
  }

  for (const name of Object.keys(expected)) {
    const expectedValue = expected[name];
    const value = object[name];
    checkValue(name, value, expectedValue);
  }
}

function checkValue(name, value, expected) {
  if (expected === null) {
    is(value, null, `'${name}' is null`);
  } else if (expected === undefined) {
    is(value, expected, `'${name}' is undefined`);
  } else if (
    typeof expected == "string" ||
    typeof expected == "number" ||
    typeof expected == "boolean"
  ) {
    is(value, expected, "property '" + name + "'");
  } else if (expected instanceof RegExp) {
    ok(expected.test(value), name + ": " + expected + " matched " + value);
  } else if (Array.isArray(expected)) {
    info("checking array for property '" + name + "'");
    ok(Array.isArray(value), `property '${name}' is an array`);

    is(value.length, expected.length, "Array has expected length");
    if (value.length !== expected.length) {
      is(JSON.stringify(value, null, 2), JSON.stringify(expected, null, 2));
    } else {
      checkObject(value, expected);
    }
  } else if (typeof expected == "object") {
    info("checking object for property '" + name + "'");
    checkObject(value, expected);
  }
}

async function triggerNetworkRequests(browser, commands) {
  for (let i = 0; i < commands.length; i++) {
    await SpecialPowers.spawn(browser, [commands[i]], async function (code) {
      const script = content.document.createElement("script");
      script.append(
        content.document.createTextNode(
          `async function triggerRequest() {${code}}`
        )
      );
      content.document.body.append(script);
      await content.wrappedJSObject.triggerRequest();
      script.remove();
    });
  }
}