mm_messageChannel.js (1928B)
1 /* eslint-env mozilla/frame-script */ 2 3 function debug(msg) { 4 dump("[mmMessageChannelChild]" + msg + "\n"); 5 } 6 7 /** 8 * Preparation Test 9 */ 10 let port; 11 let toString = Object.prototype.toString; 12 13 (function prepare() { 14 debug("Script loaded."); 15 addTestReceiver(); 16 sendAsyncMessage("mmMessagePort:finishScriptLoad"); 17 })(); 18 19 function ok(condition, message) { 20 debug("condition: " + condition + ", " + message + "\n"); 21 if (!condition) { 22 sendAsyncMessage("mmMessagePort:fail", { message }); 23 throw new Error("failed check: " + message); 24 } 25 } 26 27 function is(a, b, message) { 28 ok(a === b, message); 29 } 30 31 /** 32 * Testing codes. 33 */ 34 function addTestReceiver() { 35 addMessageListener("BasicTest:PortCreated", basicTest); 36 addMessageListener("CloseTest:PortCreated", closeTest); 37 addMessageListener("EmptyTest:PortCreated", emptyTest); 38 addMessageListener("NotTransferableTest:PortCreated", notTransferableTest); 39 } 40 41 function basicTest(msg) { 42 port = msg.ports[0]; 43 is(toString.call(port), "[object MessagePort]", "created MessagePort."); 44 45 port.onmessage = message => { 46 is(message.data, "BasicTest:StartTest", "Replied message is correct."); 47 port.postMessage("BasicTest:TestOK"); 48 }; 49 50 sendAsyncMessage("BasicTest:FinishPrepare", { message: "OK" }); 51 } 52 53 function closeTest(msg) { 54 port = msg.ports[0]; 55 is(toString.call(port), "[object MessagePort]", "created MessagePort."); 56 57 port.onmessage = message => { 58 ok(message.data, "CloseTest:StartTest", "Replied message is correct."); 59 port.postMessage("CloseTest:TestOK"); 60 }; 61 62 port.close(); 63 64 sendAsyncMessage("CloseTest:FinishPrepare", { message: "OK" }); 65 } 66 67 function emptyTest(msg) { 68 let portSize = msg.ports.length; 69 is(portSize, 0, "transfered port size is zero."); 70 71 sendAsyncMessage("EmptyTest:FinishPrepare", { message: "OK" }); 72 } 73 74 function notTransferableTest() { 75 sendAsyncMessage("NotTransferableTest:FinishPrepare", { message: "OK" }); 76 }