summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/JSProcessActor/head.js
blob: cde193ca2e9f4f5fe65b1b55299d77b0a47a253f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Provide infrastructure for JSProcessActor tests.
 */

const URL = "about:blank";
const TEST_URL = "http://test2.example.org/";
let processActorOptions = {
  jsm: {
    parent: {
      moduleURI: "resource://testing-common/TestProcessActorParent.jsm",
    },
    child: {
      moduleURI: "resource://testing-common/TestProcessActorChild.jsm",
      observers: ["test-js-content-actor-child-observer"],
    },
  },
  "sys.mjs": {
    parent: {
      esModuleURI: "resource://testing-common/TestProcessActorParent.sys.mjs",
    },
    child: {
      esModuleURI: "resource://testing-common/TestProcessActorChild.sys.mjs",
      observers: ["test-js-content-actor-child-observer"],
    },
  },
};

function promiseNotification(aNotification) {
  let notificationResolve;
  let notificationObserver = function observer() {
    notificationResolve();
    Services.obs.removeObserver(notificationObserver, aNotification);
  };
  return new Promise(resolve => {
    notificationResolve = resolve;
    Services.obs.addObserver(notificationObserver, aNotification);
  });
}

function declTest(name, cfg) {
  declTestWithOptions(name, cfg, "jsm");
  declTestWithOptions(name, cfg, "sys.mjs");
}

function declTestWithOptions(name, cfg, fileExt) {
  let {
    url = "about:blank",
    includeParent = false,
    remoteTypes,
    loadInDevToolsLoader = false,
    test,
  } = cfg;

  // Build the actor options object which will be used to register & unregister
  // our process actor.
  let actorOptions = {
    parent: Object.assign({}, processActorOptions[fileExt].parent),
    child: Object.assign({}, processActorOptions[fileExt].child),
  };
  actorOptions.includeParent = includeParent;
  if (remoteTypes !== undefined) {
    actorOptions.remoteTypes = remoteTypes;
  }
  if (loadInDevToolsLoader) {
    actorOptions.loadInDevToolsLoader = true;
  }

  // Add a new task for the actor test declared here.
  add_task(async function () {
    info("Entering test: " + name);

    // Register our actor, and load a new tab with the provided URL
    ChromeUtils.registerProcessActor("TestProcessActor", actorOptions);
    try {
      await BrowserTestUtils.withNewTab(url, async browser => {
        info("browser ready");
        await Promise.resolve(test(browser, window, fileExt));
      });
    } finally {
      // Unregister the actor after the test is complete.
      ChromeUtils.unregisterProcessActor("TestProcessActor");
      info("Exiting test: " + name);
    }
  });
}