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

"use strict";

function run_test() {
  // Allow incoming connections.
  DevToolsServer.keepAlive = true;
  DevToolsServer.init();
  DevToolsServer.registerAllActors();

  add_test(test_lazy_api);
  add_test(manual_remove);
  add_test(cleanup);
  run_next_test();
}

// Bug 988237: Test the new lazy actor actor-register
function test_lazy_api() {
  let isActorLoaded = false;
  let isActorInstantiated = false;
  function onActorEvent(subject, topic, data) {
    if (data == "loaded") {
      isActorLoaded = true;
    } else if (data == "instantiated") {
      isActorInstantiated = true;
    }
  }
  Services.obs.addObserver(onActorEvent, "actor");
  ActorRegistry.registerModule("xpcshell-test/registertestactors-lazy", {
    prefix: "lazy",
    constructor: "LazyActor",
    type: { global: true, target: true },
  });
  // The actor is immediatly registered, but not loaded
  Assert.ok(
    ActorRegistry.targetScopedActorFactories.hasOwnProperty("lazyActor")
  );
  Assert.ok(ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
  Assert.ok(!isActorLoaded);
  Assert.ok(!isActorInstantiated);

  const client = new DevToolsClient(DevToolsServer.connectPipe());
  client.connect().then(function onConnect() {
    client.mainRoot.rootForm.then(onRootForm);
  });
  function onRootForm(response) {
    // On rootForm, the actor is still not loaded,
    // but we can see its name in the list of available actors
    Assert.ok(!isActorLoaded);
    Assert.ok(!isActorInstantiated);
    Assert.ok("lazyActor" in response);

    const { LazyFront } = require("xpcshell-test/registertestactors-lazy");
    const front = new LazyFront(client);
    // As this Front isn't instantiated by protocol.js, we have to manually
    // set its actor ID and manage it:
    front.actorID = response.lazyActor;
    client.addActorPool(front);
    front.manage(front);

    front.hello().then(onRequest);
  }
  function onRequest(response) {
    Assert.equal(response, "world");

    // Finally, the actor is loaded on the first request being made to it
    Assert.ok(isActorLoaded);
    Assert.ok(isActorInstantiated);

    Services.obs.removeObserver(onActorEvent, "actor");
    client.close().then(() => run_next_test());
  }
}

function manual_remove() {
  Assert.ok(ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
  ActorRegistry.removeGlobalActor("lazyActor");
  Assert.ok(!ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));

  run_next_test();
}

function cleanup() {
  DevToolsServer.destroy();

  // Check that all actors are unregistered on server destruction
  Assert.ok(
    !ActorRegistry.targetScopedActorFactories.hasOwnProperty("lazyActor")
  );
  Assert.ok(!ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));

  run_next_test();
}