summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_add_actors.js
blob: 8077109d7172bdad1f20ae82946a12808d119ecd (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Get the object, from the server side, for a given actor ID
function getActorInstance(connID, actorID) {
  return DevToolsServer._connections[connID].getActor(actorID);
}

/**
 * The purpose of these tests is to verify that it's possible to add actors
 * both before and after the DevToolsServer has been initialized, so addons
 * that add actors don't have to poll the object for its initialization state
 * in order to add actors after initialization but rather can add actors anytime
 * regardless of the object's state.
 */
add_task(async function () {
  ActorRegistry.registerModule("resource://test/pre_init_global_actors.js", {
    prefix: "preInitGlobal",
    constructor: "PreInitGlobalActor",
    type: { global: true },
  });
  ActorRegistry.registerModule(
    "resource://test/pre_init_target_scoped_actors.js",
    {
      prefix: "preInitTargetScoped",
      constructor: "PreInitTargetScopedActor",
      type: { target: true },
    }
  );

  const client = await startTestDevToolsServer("example tab");

  ActorRegistry.registerModule("resource://test/post_init_global_actors.js", {
    prefix: "postInitGlobal",
    constructor: "PostInitGlobalActor",
    type: { global: true },
  });
  ActorRegistry.registerModule(
    "resource://test/post_init_target_scoped_actors.js",
    {
      prefix: "postInitTargetScoped",
      constructor: "PostInitTargetScopedActor",
      type: { target: true },
    }
  );

  let actors = await client.mainRoot.rootForm;
  const tabs = await client.mainRoot.listTabs();
  const tabDescriptor = tabs[0];

  // These xpcshell tests use mocked actors (xpcshell-test/testactors)
  // which still don't support watcher actor.
  // Because of that we still can't enable server side targets and target swiching.
  tabDescriptor.disableTargetSwitching();

  const tabTarget = await tabDescriptor.getTarget();

  Assert.equal(tabs.length, 1);

  let reply = await client.request({
    to: actors.preInitGlobalActor,
    type: "ping",
  });
  Assert.equal(reply.message, "pong");

  reply = await client.request({
    to: tabTarget.targetForm.preInitTargetScopedActor,
    type: "ping",
  });
  Assert.equal(reply.message, "pong");

  reply = await client.request({
    to: actors.postInitGlobalActor,
    type: "ping",
  });
  Assert.equal(reply.message, "pong");

  reply = await client.request({
    to: tabTarget.targetForm.postInitTargetScopedActor,
    type: "ping",
  });
  Assert.equal(reply.message, "pong");

  // Consider that there is only one connection, and the first one is ours
  const connID = Object.keys(DevToolsServer._connections)[0];
  const postInitGlobalActor = getActorInstance(
    connID,
    actors.postInitGlobalActor
  );
  const preInitGlobalActor = getActorInstance(
    connID,
    actors.preInitGlobalActor
  );
  actors = await client.mainRoot.getRoot();
  Assert.equal(
    postInitGlobalActor,
    getActorInstance(connID, actors.postInitGlobalActor)
  );
  Assert.equal(
    preInitGlobalActor,
    getActorInstance(connID, actors.preInitGlobalActor)
  );

  await client.close();
});