summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_register_actor.js
blob: 73ee0cedcf6ee6063d7bbcfdf5b256e985f9d8b7 (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
var gClient;

function test() {
  waitForExplicitFinish();
  var {ActorRegistryFront} = require("devtools/shared/fronts/actor-registry");
  var actorURL = "chrome://mochitests/content/chrome/devtools/server/tests/mochitest/hello-actor.js";

  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }

  gClient = new DebuggerClient(DebuggerServer.connectPipe());
  gClient.connect()
    .then(() => gClient.listTabs())
    .then(aResponse => {

      var options = {
        prefix: "helloActor",
        constructor: "HelloActor",
        type: { tab: true }
      };

      var registry = ActorRegistryFront(gClient, aResponse);
      registry.registerActor(actorURL, options).then(actorFront => {
        gClient.listTabs(response => {
          var tab = response.tabs[response.selected];
          ok(!!tab.helloActor, "Hello actor must exist");

          // Make sure actor's state is maintained across listTabs requests.
          checkActorState(tab.helloActor, () => {

            // Clean up
            actorFront.unregister().then(() => {
              gClient.close().then(() => {
                DebuggerServer.destroy();
                gClient = null;
                finish();
              });
            });
          });
        });
      });
    });
}

function checkActorState(helloActor, callback) {
  getCount(helloActor, response => {
    ok(!response.error, "No error");
    is(response.count, 1, "The counter must be valid");

    getCount(helloActor, response => {
      ok(!response.error, "No error");
      is(response.count, 2, "The counter must be valid");

      gClient.listTabs(response => {
        var tab = response.tabs[response.selected];
        is(tab.helloActor, helloActor, "Hello actor must be valid");

        getCount(helloActor, response => {
          ok(!response.error, "No error");
          is(response.count, 3, "The counter must be valid");

          callback();
        });
      });
    });
  });
}

function getCount(actor, callback) {
  gClient.request({
    to: actor,
    type: "count"
  }, callback);
}