summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/webextension-helpers.js
blob: 98f8efd900bec5e3e06f375c17c2afeed7b27c97 (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
/* exported attachAddon, waitForFramesUpdated, reloadAddon,
   collectFrameUpdates, generateWebExtensionXPI, promiseInstallFile,
   promiseWebExtensionStartup, promiseWebExtensionShutdown
 */

"use strict";

const { require, loader } = ChromeUtils.import(
  "resource://devtools/shared/Loader.jsm"
);
const { DevToolsClient } = require("devtools/client/devtools-client");
const { DevToolsServer } = require("devtools/server/devtools-server");

const {
  AddonTestUtils,
} = require("resource://testing-common/AddonTestUtils.jsm");
const {
  ExtensionTestCommon,
} = require("resource://testing-common/ExtensionTestCommon.jsm");

loader.lazyImporter(
  this,
  "ExtensionParent",
  "resource://gre/modules/ExtensionParent.jsm"
);

// Initialize a minimal DevToolsServer and connect to the webextension addon actor.
if (!DevToolsServer.initialized) {
  DevToolsServer.init();
  DevToolsServer.registerAllActors();
  SimpleTest.registerCleanupFunction(function() {
    DevToolsServer.destroy();
  });
}

SimpleTest.registerCleanupFunction(function() {
  const { hiddenXULWindow } = ExtensionParent.DebugUtils;
  const debugBrowserMapSize =
    ExtensionParent.DebugUtils.debugBrowserPromises.size;

  if (debugBrowserMapSize > 0) {
    is(
      debugBrowserMapSize,
      0,
      "ExtensionParent DebugUtils debug browsers have not been released"
    );
  }

  if (hiddenXULWindow) {
    ok(
      false,
      "ExtensionParent DebugUtils hiddenXULWindow has not been destroyed"
    );
  }
});

// Test helpers related to the webextensions debugging RDP actors.

function waitForFramesUpdated(target, matchFn) {
  return new Promise(resolve => {
    const listener = data => {
      if (typeof matchFn === "function" && !matchFn(data)) {
        return;
      } else if (!data.frames) {
        return;
      }

      target.off("frameUpdate", listener);
      resolve(data.frames);
    };
    target.on("frameUpdate", listener);
  });
}

function collectFrameUpdates({ client }, matchFn) {
  const collected = [];

  const listener = data => {
    if (matchFn(data)) {
      collected.push(data);
    }
  };

  client.on("frameUpdate", listener);
  let unsubscribe = () => {
    unsubscribe = null;
    client.off("frameUpdate", listener);
    return collected;
  };

  SimpleTest.registerCleanupFunction(function() {
    if (unsubscribe) {
      unsubscribe();
    }
  });

  return unsubscribe;
}

async function attachAddon(addonId) {
  const transport = DevToolsServer.connectPipe();
  const client = new DevToolsClient(transport);

  await client.connect();

  const addonDescriptor = await client.mainRoot.getAddon({ id: addonId });
  const addonTarget = await addonDescriptor.getTarget();

  if (!addonTarget) {
    client.close();
    throw new Error(`No WebExtension Actor found for ${addonId}`);
  }

  return addonTarget;
}

async function reloadAddon({ client }, addonId) {
  const addonTargetFront = await client.mainRoot.getAddon({ id: addonId });

  if (!addonTargetFront) {
    client.close();
    throw new Error(`No WebExtension Actor found for ${addonId}`);
  }

  await addonTargetFront.reload();
}

// Test helpers related to the AddonManager.

function generateWebExtensionXPI(extDetails) {
  return ExtensionTestCommon.generateXPI(extDetails);
}

let {
  promiseInstallFile,
  promiseWebExtensionStartup,
  promiseWebExtensionShutdown,
} = AddonTestUtils;