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

// Test the DebuggerClient.registerClient API

var EventEmitter = require("devtools/shared/event-emitter");

var gClient;
var gActors;
var gTestClient;

function TestActor(conn) {
  this.conn = conn;
}
TestActor.prototype = {
  actorPrefix: "test",

  start: function () {
    this.conn.sendActorEvent(this.actorID, "foo", {
      hello: "world"
    });
    return {};
  }
};
TestActor.prototype.requestTypes = {
  "start": TestActor.prototype.start
};

function TestClient(client, form) {
  this.client = client;
  this.actor = form.test;
  this.events = ["foo"];
  EventEmitter.decorate(this);
  client.registerClient(this);

  this.detached = false;
}
TestClient.prototype = {
  start: function () {
    this.client.request({
      to: this.actor,
      type: "start"
    });
  },

  detach: function (onDone) {
    this.detached = true;
    onDone();
  }
};

function run_test()
{
  DebuggerServer.addGlobalActor(TestActor);

  DebuggerServer.init();
  DebuggerServer.addBrowserActors();

  add_test(init);
  add_test(test_client_events);
  add_test(close_client);
  run_next_test();
}

function init()
{
  gClient = new DebuggerClient(DebuggerServer.connectPipe());
  gClient.connect()
    .then(() => gClient.listTabs())
    .then(aResponse => {
      gActors = aResponse;
      gTestClient = new TestClient(gClient, aResponse);
      run_next_test();
    });
}

function test_client_events()
{
  // Test DebuggerClient.registerClient and DebuggerServerConnection.sendActorEvent
  gTestClient.on("foo", function (type, data) {
    do_check_eq(type, "foo");
    do_check_eq(data.hello, "world");
    run_next_test();
  });
  gTestClient.start();
}

function close_client() {
  gClient.close().then(() => {
    // Check that client.detach method is call on client destruction
    do_check_true(gTestClient.detached);
    run_next_test();
  });
}