summaryrefslogtreecommitdiffstats
path: root/dom/messagechannel/tests/mm_messageChannel.js
blob: 8cc104e748bc8addb55e5681fb9bfa401a86ca12 (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
/* eslint-env mozilla/frame-script */

function debug(msg) {
  dump("[mmMessageChannelChild]" + msg + "\n");
}

/**
 * Preparation Test
 */
let port;
let toString = Object.prototype.toString;

(function prepare() {
  debug("Script loaded.");
  addTestReceiver();
  sendAsyncMessage("mmMessagePort:finishScriptLoad");
})();

function ok(condition, message) {
  debug("condition: " + condition + ", " + message + "\n");
  if (!condition) {
    sendAsyncMessage("mmMessagePort:fail", { message });
    throw new Error("failed check: " + message);
  }
}

function is(a, b, message) {
  ok(a === b, message);
}

/**
 * Testing codes.
 */
function addTestReceiver() {
  addMessageListener("BasicTest:PortCreated", basicTest);
  addMessageListener("CloseTest:PortCreated", closeTest);
  addMessageListener("EmptyTest:PortCreated", emptyTest);
  addMessageListener("NotTransferableTest:PortCreated", notTransferableTest);
}

function basicTest(msg) {
  port = msg.ports[0];
  is(toString.call(port), "[object MessagePort]", "created MessagePort.");

  port.onmessage = message => {
    is(message.data, "BasicTest:StartTest", "Replied message is correct.");
    port.postMessage("BasicTest:TestOK");
  };

  sendAsyncMessage("BasicTest:FinishPrepare", { message: "OK" });
}

function closeTest(msg) {
  port = msg.ports[0];
  is(toString.call(port), "[object MessagePort]", "created MessagePort.");

  port.onmessage = message => {
    ok(message.data, "CloseTest:StartTest", "Replied message is correct.");
    port.postMessage("CloseTest:TestOK");
  };

  port.close();

  sendAsyncMessage("CloseTest:FinishPrepare", { message: "OK" });
}

function emptyTest(msg) {
  let portSize = msg.ports.length;
  is(portSize, 0, "transfered port size is zero.");

  sendAsyncMessage("EmptyTest:FinishPrepare", { message: "OK" });
}

function notTransferableTest() {
  sendAsyncMessage("NotTransferableTest:FinishPrepare", { message: "OK" });
}