mm_messageChannel.js 1.9 KB

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