summaryrefslogtreecommitdiffstats
path: root/dom/messagechannel/tests/mm_messageChannelParent.js
blob: f103a1746b4cf29870f251df762497a5e9a5a614 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
let port;
let mm;

function info(message) {
  return window.arguments[0].info(message);
}

function ok(condition, message) {
  return window.arguments[0].ok(condition, message);
}

function is(v1, v2, message) {
  return window.arguments[0].is(v1, v2, message);
}

function todo_is(v1, v2, message) {
  return window.arguments[0].todo_is(v1, v2, message);
}

function cleanUp() {
  window.arguments[0].setTimeout(function () {
    this.done();
  }, 0);
  window.close();
}

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

let tests = [basic_test, close_test, empty_transferable, not_transferable];

// Test Routine
function run_tests() {
  let test = tests.shift();
  if (test === undefined) {
    cleanUp();
    return;
  }

  test(function () {
    setTimeout(run_tests, 0);
  });
}

// Basic communication test.
function basic_test(finish) {
  ok(mm, "basic_test");

  let finishPrepare = msg => {
    is(msg.data.message, "OK", "");
    ok(port, "");
    port.onmessage = message => {
      is(message.data, "BasicTest:TestOK", "");
      finish();
    };
    port.postMessage("BasicTest:StartTest");
    mm.removeMessageListener("BasicTest:FinishPrepare", finishPrepare);
  };

  let channel = new MessageChannel();
  port = channel.port2;
  mm.addMessageListener("BasicTest:FinishPrepare", finishPrepare);
  mm.sendAsyncMessage("BasicTest:PortCreated", {}, [channel.port1]);
}

// Communicate with closed port.
function close_test(finish) {
  ok(mm, "close_test");

  let finishPrepare = msg => {
    is(msg.data.message, "OK", "");
    ok(port, "");

    port.onmessage = () => {
      ok(false, "Port is alive.");
      finish();
    };

    port.postMessage("CloseTest:StartTest");
    mm.removeMessageListener("CloseTest:FinishPrepare", finishPrepare);
    finish();
  };

  let channel = new MessageChannel();
  port = channel.port2;
  mm.addMessageListener("CloseTest:FinishPrepare", finishPrepare);
  mm.sendAsyncMessage("CloseTest:PortCreated", {}, [channel.port1]);
}

// Empty transferable object
function empty_transferable(finish) {
  ok(mm, "empty_transferable");

  let finishPrepare = () => {
    ok(true, "Same basic test.");
    mm.removeMessageListener("EmptyTest:FinishPrepare", finishPrepare);
    finish();
  };

  mm.addMessageListener("EmptyTest:FinishPrepare", finishPrepare);
  mm.sendAsyncMessage("EmptyTest:PortCreated", {}, []);
}

// Not transferable object.
function not_transferable(finish) {
  ok(mm, "not_transferable");

  let finishPrepare = () => {
    ok(true, "Same basic test.");
    finish();
  };

  mm.addMessageListener("NotTransferableTest:FinishPrepare", finishPrepare);
  mm.sendAsyncMessage("NotTransferableTest:PortCreated", {}, [""]);
}

/*
 * Test preparation
 */
function finishLoad() {
  run_tests();
}

function prepare_test() {
  debug("start run_tests()");
  var node = document.getElementById("messagechannel_remote");
  mm = node.messageManager; //Services.ppmm.getChildAt(1);
  ok(mm, "created MessageManager.");

  mm.addMessageListener("mmMessagePort:finishScriptLoad", finishLoad);
  mm.addMessageListener("mmMessagePort:fail", failed_test);
  //mm.loadProcessScript("chrome://mochitests/content/chrome/dom/messagechannel/tests/mm_messageChannel.js", true);
  mm.loadFrameScript(
    "chrome://mochitests/content/chrome/dom/messagechannel/tests/mm_messageChannel.js",
    true
  );
  ok(true, "Loaded");
}

function failed_test() {
  debug("failed test in child process");
  ok(false, "");
}