summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_transport_events.js
blob: 1e2b67ac48f7c2d7014a5a4e7477097e7dfa0b63 (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
108
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { on, off } = require("sdk/event/core");
const { DebuggerClient } = require("devtools/shared/client/main");

function test() {
  gDevTools.on("toolbox-created", onToolboxCreated);
  on(DebuggerClient, "connect", onDebuggerClientConnect);

  addTab("about:blank").then(function () {
    let target = TargetFactory.forTab(gBrowser.selectedTab);
    gDevTools.showToolbox(target, "webconsole").then(testResults);
  });
}

function testResults(toolbox) {
  testPackets(sent1, received1);
  testPackets(sent2, received2);

  cleanUp(toolbox);
}

function cleanUp(toolbox) {
  gDevTools.off("toolbox-created", onToolboxCreated);
  off(DebuggerClient, "connect", onDebuggerClientConnect);

  toolbox.destroy().then(function () {
    gBrowser.removeCurrentTab();
    executeSoon(function () {
      finish();
    });
  });
}

function testPackets(sent, received) {
  ok(sent.length > 0, "There must be at least one sent packet");
  ok(received.length > 0, "There must be at leaset one received packet");

  if (!sent.length || received.length) {
    return;
  }

  let sentPacket = sent[0];
  let receivedPacket = received[0];

  is(receivedPacket.from, "root",
    "The first received packet is from the root");
  is(receivedPacket.applicationType, "browser",
    "The first received packet has browser type");
  is(sentPacket.type, "listTabs",
    "The first sent packet is for list of tabs");
}

// Listen to the transport object that is associated with the
// default Toolbox debugger client
var sent1 = [];
var received1 = [];

function send1(eventId, packet) {
  sent1.push(packet);
}

function onPacket1(eventId, packet) {
  received1.push(packet);
}

function onToolboxCreated(eventId, toolbox) {
  toolbox.target.makeRemote();
  let client = toolbox.target.client;
  let transport = client._transport;

  transport.on("send", send1);
  transport.on("packet", onPacket1);

  client.addOneTimeListener("closed", event => {
    transport.off("send", send1);
    transport.off("packet", onPacket1);
  });
}

// Listen to all debugger client object protocols.
var sent2 = [];
var received2 = [];

function send2(eventId, packet) {
  sent2.push(packet);
}

function onPacket2(eventId, packet) {
  received2.push(packet);
}

function onDebuggerClientConnect(client) {
  let transport = client._transport;

  transport.on("send", send2);
  transport.on("packet", onPacket2);

  client.addOneTimeListener("closed", event => {
    transport.off("send", send2);
    transport.off("packet", onPacket2);
  });
}