diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/messagechannel/tests | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/messagechannel/tests')
42 files changed, 1999 insertions, 0 deletions
diff --git a/dom/messagechannel/tests/.eslintrc.js b/dom/messagechannel/tests/.eslintrc.js new file mode 100644 index 0000000000..721e0938af --- /dev/null +++ b/dom/messagechannel/tests/.eslintrc.js @@ -0,0 +1,5 @@ +"use strict"; + +module.exports = { + extends: ["plugin:mozilla/mochitest-test", "plugin:mozilla/chrome-test"], +}; diff --git a/dom/messagechannel/tests/chrome.ini b/dom/messagechannel/tests/chrome.ini new file mode 100644 index 0000000000..9ec2edcb4d --- /dev/null +++ b/dom/messagechannel/tests/chrome.ini @@ -0,0 +1,12 @@ +[DEFAULT] +support-files = + iframe_messageChannel_chrome.html + mm_messageChannelParent.xhtml + mm_messageChannelParentNotRemote.xhtml + mm_messageChannelParent.js + mm_messageChannel.js + +[test_messageChannel.xhtml] +[test_messageChannelWithMessageManager.xhtml] +skip-if = os == 'android' +[test_messageChannelWithMessageManagerNotRemote.xhtml] diff --git a/dom/messagechannel/tests/iframe_messageChannel_chrome.html b/dom/messagechannel/tests/iframe_messageChannel_chrome.html new file mode 100644 index 0000000000..5fcd43a34c --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_chrome.html @@ -0,0 +1,11 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + +window.addEventListener('message', receiveMessage); +function receiveMessage(evt) { + evt.data.postMessage("Hello world"); +} + </script> +</body> diff --git a/dom/messagechannel/tests/iframe_messageChannel_cloning.html b/dom/messagechannel/tests/iframe_messageChannel_cloning.html new file mode 100644 index 0000000000..3012f9a845 --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_cloning.html @@ -0,0 +1,21 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + + function ok(a, msg) { + window.parent.postMessage({ status: a ? "OK" : "KO", message: msg }, "*"); + } + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + ok (evt.data, "Data received"); + ok (evt.data.port instanceof MessagePort, "Data contains a MessagePort"); + + var a = new MessageChannel(); + window.parent.postMessage({ status: "FINISH", port: a.port2 }, '*', [a.port2]); + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/iframe_messageChannel_pingpong.html b/dom/messagechannel/tests/iframe_messageChannel_pingpong.html new file mode 100644 index 0000000000..c4decd35a7 --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_pingpong.html @@ -0,0 +1,32 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + + function ok(what, msg) { + window.parent.postMessage({type: what ? 'OK' : 'KO', msg }, '*'); + } + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + if (evt.data.type == 'PORT') { + var port = evt.data.port; + var counter = 0; + port.onmessage = function(event) { + if (counter++ == 0) { + ok(!(event.data % 2), "The number " + event.data + " has been received correctly by the iframe"); + + window.parent.postMessage({ type: 'PORT', port }, '*', [port]); + } + else { + ok(false, "Wrong message!"); + } + } + } else { + ok(false, "Unknown message"); + } + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/iframe_messageChannel_post.html b/dom/messagechannel/tests/iframe_messageChannel_post.html new file mode 100644 index 0000000000..846b247f0b --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_post.html @@ -0,0 +1,23 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + + var port; + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + port = evt.data.port; + + port.addEventListener('message', receivePostMessage); + function receivePostMessage(event) { + port.postMessage(event.data); + } + port.start(); + + window.parent.postMessage({ status: "READY" }, '*'); + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/iframe_messageChannel_sharedWorker2.html b/dom/messagechannel/tests/iframe_messageChannel_sharedWorker2.html new file mode 100644 index 0000000000..b96026955d --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_sharedWorker2.html @@ -0,0 +1,13 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + + var a = new SharedWorker('sharedWorker2_messageChannel.js'); + a.port.onmessage = function(evt) { + evt.ports[0].postMessage("Hello from the iframe!"); + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/iframe_messageChannel_transferable.html b/dom/messagechannel/tests/iframe_messageChannel_transferable.html new file mode 100644 index 0000000000..0a00add71f --- /dev/null +++ b/dom/messagechannel/tests/iframe_messageChannel_transferable.html @@ -0,0 +1,24 @@ +<!DOCTYPE HTML> +<html> +<body> + <script type="application/javascript"> + + function ok(what, msg) { + window.parent.postMessage({type: what ? 'OK' : 'KO', msg }, '*'); + } + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + ok(evt.ports.length == 1, "Port transferred!"); + + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + evt.ports[0].postMessage('hello world!', [a.port2]); + a.port1.onmessage = function(event) { + event.target.postMessage(event.data); + } + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/mm_messageChannel.js b/dom/messagechannel/tests/mm_messageChannel.js new file mode 100644 index 0000000000..443a7949e2 --- /dev/null +++ b/dom/messagechannel/tests/mm_messageChannel.js @@ -0,0 +1,74 @@ +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 "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(msg) { + sendAsyncMessage("NotTransferableTest:FinishPrepare", { message: "OK" }); +} diff --git a/dom/messagechannel/tests/mm_messageChannelParent.js b/dom/messagechannel/tests/mm_messageChannelParent.js new file mode 100644 index 0000000000..a669b81073 --- /dev/null +++ b/dom/messagechannel/tests/mm_messageChannelParent.js @@ -0,0 +1,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 = message => { + 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 = msg => { + 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 = msg => { + ok(true, "Same basic test."); + finish(); + }; + + mm.addMessageListener("NotTransferableTest:FinishPrepare", finishPrepare); + mm.sendAsyncMessage("NotTransferableTest:PortCreated", {}, [""]); +} + +/* + * Test preparation + */ +function finishLoad(msg) { + 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, ""); +} diff --git a/dom/messagechannel/tests/mm_messageChannelParent.xhtml b/dom/messagechannel/tests/mm_messageChannelParent.xhtml new file mode 100644 index 0000000000..5f5c5ae5be --- /dev/null +++ b/dom/messagechannel/tests/mm_messageChannelParent.xhtml @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> +<window title="Test MessageChannel API with nsFrameMessageManager(bug 1174624)" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + onload="prepare_test()"> + + <!-- test code goes here --> + <script type="application/javascript" + src="chrome://mochitests/content/chrome/dom/messagechannel/tests/mm_messageChannelParent.js"></script> + <browser type="content" src="about:blank" id="messagechannel_remote" remote="true"/> +</window> diff --git a/dom/messagechannel/tests/mm_messageChannelParentNotRemote.xhtml b/dom/messagechannel/tests/mm_messageChannelParentNotRemote.xhtml new file mode 100644 index 0000000000..cb3b55dea7 --- /dev/null +++ b/dom/messagechannel/tests/mm_messageChannelParentNotRemote.xhtml @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> +<window title="Test MessageChannel API with nsFrameMessageManager(bug 1174624)" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + onload="prepare_test()"> + + <!-- test code goes here --> + <script type="application/javascript" + src="chrome://mochitests/content/chrome/dom/messagechannel/tests/mm_messageChannelParent.js"></script> + <browser type="content" src="about:blank" id="messagechannel_remote"/> +</window> diff --git a/dom/messagechannel/tests/mochitest.ini b/dom/messagechannel/tests/mochitest.ini new file mode 100644 index 0000000000..20862a1c25 --- /dev/null +++ b/dom/messagechannel/tests/mochitest.ini @@ -0,0 +1,32 @@ +[DEFAULT] +support-files = + iframe_messageChannel_cloning.html + iframe_messageChannel_pingpong.html + iframe_messageChannel_post.html + iframe_messageChannel_transferable.html + worker_messageChannel.js + worker_messageChannel_any.js + sharedWorker_messageChannel.js + sharedWorker2_messageChannel.js + iframe_messageChannel_sharedWorker2.html + !/dom/events/test/event_leak_utils.js + +[test_event_listener_leaks.html] +skip-if = (os == "win" && processor == "aarch64") #bug 1535784 +[test_messageChannel.html] +[test_messageChannel_cloning.html] +[test_messageChannel_pingpong.html] +[test_messageChannel_post.html] +[test_messageChannel_start.html] +[test_messageChannel_transferable.html] +[test_messageChannel_unshipped.html] +[test_messageChannel_worker.html] +[test_messageChannel_selfTransferring.html] +[test_messageChannel_sharedWorker.html] +[test_messageChannel_sharedWorker2.html] +[test_messageChannel_any.html] +[test_messageChannel_forceClose.html] +[test_messageChannel_bug1178076.html] +[test_messageChannel_bug1224825.html] +[test_messageChannel_worker_forceClose.html] +[test_removedWindow.html] diff --git a/dom/messagechannel/tests/moz.build b/dom/messagechannel/tests/moz.build new file mode 100644 index 0000000000..6cf89eae3b --- /dev/null +++ b/dom/messagechannel/tests/moz.build @@ -0,0 +1,10 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +MOCHITEST_MANIFESTS += ["mochitest.ini"] +MOCHITEST_CHROME_MANIFESTS += ["chrome.ini"] + +XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"] diff --git a/dom/messagechannel/tests/sharedWorker2_messageChannel.js b/dom/messagechannel/tests/sharedWorker2_messageChannel.js new file mode 100644 index 0000000000..6b1280bd6c --- /dev/null +++ b/dom/messagechannel/tests/sharedWorker2_messageChannel.js @@ -0,0 +1,7 @@ +var mc = new MessageChannel(); +var i = 0; + +onconnect = function(evt) { + dump("CONNECTING: " + i + "\n"); + evt.ports[0].postMessage(42, [mc["port" + ++i]]); +}; diff --git a/dom/messagechannel/tests/sharedWorker_messageChannel.js b/dom/messagechannel/tests/sharedWorker_messageChannel.js new file mode 100644 index 0000000000..a3abfaa170 --- /dev/null +++ b/dom/messagechannel/tests/sharedWorker_messageChannel.js @@ -0,0 +1,8 @@ +onconnect = function(evt) { + var mc = new MessageChannel(); + + evt.ports[0].postMessage(42, [mc.port2]); + mc.port1.onmessage = function(e) { + mc.port1.postMessage(e.data); + }; +}; diff --git a/dom/messagechannel/tests/test_event_listener_leaks.html b/dom/messagechannel/tests/test_event_listener_leaks.html new file mode 100644 index 0000000000..93ed5215d3 --- /dev/null +++ b/dom/messagechannel/tests/test_event_listener_leaks.html @@ -0,0 +1,50 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Bug 1450358 - Test MessageChannel event listener leak conditions</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="/tests/dom/events/test/event_leak_utils.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<script class="testbody" type="text/javascript"> +// Manipulate MessageChannel objects in the frame's context. +// Its important here that we create a listener callback from +// the DOM objects back to the frame's global in order to +// exercise the leak condition. +async function useMessagePort(contentWindow) { + contentWindow.messageCount = 0; + + let mc = new contentWindow.MessageChannel + mc.port1.postMessage("foo"); + + await new Promise(resolve => { + mc.port2.onmessage = e => { + contentWindow.messageCount += 1; + resolve(); + }; + }); + + is(contentWindow.messageCount, 1, "message should be received"); +} + +async function runTest() { + try { + await checkForEventListenerLeaks("MessageChannel", useMessagePort); + } catch (e) { + ok(false, e); + } finally { + SimpleTest.finish(); + } +} + +SimpleTest.waitForExplicitFinish(); +addEventListener("load", runTest, { once: true }); +</script> +</pre> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel.html b/dom/messagechannel/tests/test_messageChannel.html new file mode 100644 index 0000000000..ce6774d33c --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel.html @@ -0,0 +1,43 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - basic support</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + /** Test for Bug 677638 **/ + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var port1 = a.port1; + ok(port1, "MessageChannel.port1 exists"); + is(port1, a.port1, "MessageChannel.port1 is port1"); + + var port2 = a.port2; + ok(port2, "MessageChannel.port1 exists"); + is(port2, a.port2, "MessageChannel.port2 is port2"); + + [ 'postMessage', 'start', 'close' ].forEach(function(e) { + ok(e in port1, "MessagePort1." + e + " exists"); + ok(e in port2, "MessagePort2." + e + " exists"); + }); + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel.xhtml b/dom/messagechannel/tests/test_messageChannel.xhtml new file mode 100644 index 0000000000..e51359d67d --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel.xhtml @@ -0,0 +1,38 @@ +<?xml version="1.0"?> +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> +<window title="Test for MessageChannel API" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> + + <!-- test results are displayed in the html:body --> + <body xmlns="http://www.w3.org/1999/xhtml" id="body"> + </body> + + <!-- test code goes here --> + <script type="application/javascript"><![CDATA[ + + ok("MessageChannel" in window, "Should MessageChannel exist?"); + + var channel = new MessageChannel(); + ok(channel, "MessageChannel is created"); + + channel.port1.onmessage = function(evt) { + ok(true, "message received!"); + SimpleTest.finish(); + } + + var ifr = document.createXULElement('browser'); + ifr.setAttribute("src", "iframe_messageChannel_chrome.html"); + ifr.setAttribute("flex", "1"); + ifr.addEventListener('load', function() { + ifr.contentWindow.postMessage(channel.port2, '*', [channel.port2]); + }); + + var body = document.getElementById("body"); + body.appendChild(ifr); + + SimpleTest.waitForExplicitFinish(); + + ]]></script> +</window> diff --git a/dom/messagechannel/tests/test_messageChannelWithMessageManager.xhtml b/dom/messagechannel/tests/test_messageChannelWithMessageManager.xhtml new file mode 100644 index 0000000000..b95a85645c --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannelWithMessageManager.xhtml @@ -0,0 +1,27 @@ +<?xml version="1.0"?> +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" + type="text/css"?> +<window title="Test MessageChannel API with nsFrameMessageManager(bug 1174624)" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + + <!-- test results are displayed in the html:body --> + <body xmlns="http://www.w3.org/1999/xhtml"> + </body> + + <!-- test code goes here --> + <script type="application/javascript"><![CDATA[ + + SimpleTest.waitForExplicitFinish(); + + function done() { + info("done called"); + SimpleTest.finish(); + } + + addLoadEvent(function() { + window.openDialog("mm_messageChannelParent.xhtml", "", "chrome,noopener", window); + }); + ]]></script> +</window> diff --git a/dom/messagechannel/tests/test_messageChannelWithMessageManagerNotRemote.xhtml b/dom/messagechannel/tests/test_messageChannelWithMessageManagerNotRemote.xhtml new file mode 100644 index 0000000000..e87fd4d873 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannelWithMessageManagerNotRemote.xhtml @@ -0,0 +1,27 @@ +<?xml version="1.0"?> +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" + type="text/css"?> +<window title="Test MessageChannel API with nsFrameMessageManager(bug 1174624)" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + + <!-- test results are displayed in the html:body --> + <body xmlns="http://www.w3.org/1999/xhtml"> + </body> + + <!-- test code goes here --> + <script type="application/javascript"><![CDATA[ + + SimpleTest.waitForExplicitFinish(); + + function done() { + info("done called"); + SimpleTest.finish(); + } + + addLoadEvent(function() { + window.openDialog("mm_messageChannelParentNotRemote.xhtml", "", "chrome,noopener", window); + }); + ]]></script> +</window> diff --git a/dom/messagechannel/tests/test_messageChannel_any.html b/dom/messagechannel/tests/test_messageChannel_any.html new file mode 100644 index 0000000000..ff83c55288 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_any.html @@ -0,0 +1,115 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>MessagePort/Channel any content</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + +var tests = [ + 'hello world', + 123, + null, + true, + new Date(), + [ 1, 'test', true, new Date() ], + { a: true, b: null, c: new Date(), d: [ true, false, {} ] }, + new Blob([123], { type: 'plain/text' }) +]; + +var currentTest = null; + +function getType(a) { + if (a === null || a === undefined) + return 'null'; + + if (Array.isArray(a)) + return 'array'; + + if (typeof a == 'object') + return 'object'; + + return 'primitive'; +} + +function compare(a, b) { + is (getType(a), getType(b), 'Type matches'); + + var type = getType(a); + if (type == 'array') { + is (a.length, b.length, 'Array.length matches'); + for (var i = 0; i < a.length; ++i) { + compare(a[i], b[i]); + } + + return; + } + + if (type == 'object') { + ok (a !== b, 'They should not match'); + + var aProps = []; + for (var p in a) aProps.push(p); + + var bProps = []; + for (var p in b) bProps.push(p); + + is (aProps.length, bProps.length, 'Props match'); + is (aProps.sort().toString(), bProps.sort().toString(), 'Prop names match'); + + for (var p in a) { + compare(a[p], b[p]); + } + + return; + } + + if (type != 'null') { + is (a, b, 'Same value'); + } +} + +function runTest() { + var mc = new MessageChannel('foobar'); + ok(mc, "MessageChannel can be created"); + + mc.port1.onmessage = function(event) { + compare(event.data, currentTest); + next(); + } + + function next() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + currentTest = tests.shift(); + mc.port1.postMessage(currentTest); + } + + var worker = new Worker("worker_messageChannel_any.js"); + worker.onmessage = function(event) { + if (event.data == "READY") { + next(); + } + }; + + worker.postMessage(mc.port2, [mc.port2]); +} + +SimpleTest.waitForExplicitFinish(); +runTest(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_bug1178076.html b/dom/messagechannel/tests/test_messageChannel_bug1178076.html new file mode 100644 index 0000000000..e8d4d3b9fb --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_bug1178076.html @@ -0,0 +1,38 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1178076 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1178076</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1178076">Mozilla Bug 1178076</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function runTest() { + onmessage = function(e) { + is(e.ports.length, 1, "A port has been received!"); + var port = e.ports[0]; + ok(port instanceof MessagePort, "This is a port."); + SimpleTest.finish(); + } + + // In this test we want to see if we leak a neutered port closing port1 + // and sending port2 to the same window. This operation doesn't involve IPC. + var mc = new MessageChannel(); + mc.port1.close(); + postMessage(42, '*', [mc.port2]); + } + + SimpleTest.waitForExplicitFinish(); + runTest(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_bug1224825.html b/dom/messagechannel/tests/test_messageChannel_bug1224825.html new file mode 100644 index 0000000000..90b1b51076 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_bug1224825.html @@ -0,0 +1,94 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1224825 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1224825</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1224825">Mozilla Bug 1224825</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + +var MAX = 100; + +function test_fullDeliveredMessages() { + var worker = new Worker('data:javascript,onmessage = function(e) { e.ports[0].onmessage = function(evt) { postMessage(evt.data);}}'); + + var count = 0; + worker.onmessage = function(e) { + is(e.data, count, "Correct value expected!"); + ok(count < MAX,"No count > MAX messages!"); + if (++count == MAX) { + + SimpleTest.requestFlakyTimeout("Testing an event not happening"); + setTimeout(function() { + runTests(); + }, 200); + + info("All the messages correctly received"); + } + } + + var mc = new MessageChannel(); + worker.postMessage(42, [mc.port2]); + + for (var i = 0; i < MAX; ++i) { + mc.port1.postMessage(i); + } + + mc.port1.close(); + + for (var i = 0; i < MAX * 2; ++i) { + mc.port1.postMessage(i); + } +} + +function test_closeInBetween() { + var mc = new MessageChannel(); + + for (var i = 0; i < MAX; ++i) { + mc.port1.postMessage(i); + } + + mc.port1.onmessage = function(e) { + ok (e.data < MAX/2, "Correct message received from port1:" + e.data); + } + + mc.port2.onmessage = function(e) { + ok (e.data < MAX, "Correct message received from port2:" + e.data); + if (e.data == MAX/2) { + mc.port2.close(); + } + + mc.port2.postMessage(e.data); + + if (e.data == MAX - 1) { + runTests(); + } + } +} + +var tests = [ test_fullDeliveredMessages, test_closeInBetween ]; + +function runTests() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); +} + +SimpleTest.waitForExplicitFinish(); +runTests(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_cloning.html b/dom/messagechannel/tests/test_messageChannel_cloning.html new file mode 100644 index 0000000000..7f59e9e96f --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_cloning.html @@ -0,0 +1,70 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - port cloning</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + // This test checks if MessagePorts can be shared with iframes + function test_iframe() { + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + if (evt.data.status == 'OK') { + ok(true, evt.data.message); + } else if (evt.data.status == 'KO') { + ok(false, evt.data.message); + } else if (evt.data.status == 'FINISH') { + ok (evt.data.port instanceof MessagePort, "Data contains a MessagePort"); + window.removeEventListener('message', receiveMessage); + runTest(); + } else { + ok(false, "Unknown message"); + } + } + + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var div = document.getElementById("content"); + ok(div, "Parent exists"); + + var ifr = document.createElement("iframe"); + ifr.addEventListener("load", iframeLoaded); + ifr.setAttribute('src', "iframe_messageChannel_cloning.html"); + div.appendChild(ifr); + + function iframeLoaded() { + ifr.contentWindow.postMessage({ port: a.port2 }, '*', [a.port2]); + } + } + + var tests = [ + test_iframe + ]; + + function runTest() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); + } + + SimpleTest.waitForExplicitFinish(); + runTest(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_forceClose.html b/dom/messagechannel/tests/test_messageChannel_forceClose.html new file mode 100644 index 0000000000..98c9e87311 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_forceClose.html @@ -0,0 +1,30 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1176034 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1176034 - start/close</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1176034">Mozilla Bug 1176034</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + var mc = new MessageChannel(); + + try { + postMessage(42, "*", [ mc.port1, window ]); + ok(false, "Something went wrong."); + } catch(e) { + ok(true, "PostMessage should fail and we should not leak."); + } + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_pingpong.html b/dom/messagechannel/tests/test_messageChannel_pingpong.html new file mode 100644 index 0000000000..602478bcb7 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_pingpong.html @@ -0,0 +1,77 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - port cloning</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function runTest() { + var MAX = 100; + + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + // Populate the message queue of this port. + for (var i = 0; i < MAX; ++i) { + a.port1.postMessage(i); + } + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + + // This test sends the port from this window to the iframe and viceversa. + if (evt.data.type == 'PORT') { + var port = evt.data.port; + var counter = 0; + port.onmessage = function(event) { + // only 1 message should be received by this port. + if (counter++ == 0) { + ok(event.data % 2, "The number " + event.data + " has been received correctly by the main window"); + + if (event.data < MAX - 1) { + ifr.contentWindow.postMessage({ type: 'PORT', port }, '*', [port]); + } else { + SimpleTest.finish(); + } + } else { + ok(false, "Wrong message!"); + } + } + } else if (evt.data.type == 'OK') { + ok(true, evt.data.msg); + } else if (evt.data.type == 'KO') { + ok(false, evt.data.msg); + } else { + ok(false, "Unknown message"); + } + } + + var div = document.getElementById("content"); + ok(div, "Parent exists"); + + var ifr = document.createElement("iframe"); + ifr.addEventListener("load", iframeLoaded); + ifr.setAttribute('src', "iframe_messageChannel_pingpong.html"); + div.appendChild(ifr); + + function iframeLoaded() { + ifr.contentWindow.postMessage({ type: 'PORT', port: a.port2 }, '*', [a.port2]); + } + } + + SimpleTest.waitForExplicitFinish(); + runTest(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_post.html b/dom/messagechannel/tests/test_messageChannel_post.html new file mode 100644 index 0000000000..86c27efc3e --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_post.html @@ -0,0 +1,76 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - port cloning</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function start() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + if (evt.data.status == 'READY') { + runTest(); + } else { + ok(false, "Unknown message"); + } + } + + var div = document.getElementById("content"); + ok(div, "Parent exists"); + + var ifr = document.createElement("iframe"); + ifr.addEventListener("load", iframeLoaded); + ifr.setAttribute('src', "iframe_messageChannel_post.html"); + div.appendChild(ifr); + + function iframeLoaded() { + ifr.contentWindow.postMessage({ port: a.port2 }, '*', [a.port2]); + } + + var tests = [ 42, + null, + undefined, + "hello world", + new Blob([]), + true ]; + + a.port1.onmessage = function(evt) { + ok(tests.length, "We are waiting for a message"); + if (typeof(tests[0]) == 'object') { + is(typeof(tests[0]), typeof(evt.data), "Value ok: " + tests[0]); + } else { + is(tests[0], evt.data, "Value ok: " + tests[0]); + } + tests.shift(); + runTest(); + } + + function runTest() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + a.port1.postMessage(tests[0]); + } + } + + SimpleTest.waitForExplicitFinish(); + start(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_selfTransferring.html b/dom/messagechannel/tests/test_messageChannel_selfTransferring.html new file mode 100644 index 0000000000..c3b96f32e6 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_selfTransferring.html @@ -0,0 +1,32 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>MessagePort/Channel no self tranferring</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + var a = new MessageChannel(); + + var status = false; + try { + a.port1.postMessage('foobar', [a.port1]); + } catch(e) { + status =true; + } + + ok(status, "Transfering the same port should throw"); + + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_sharedWorker.html b/dom/messagechannel/tests/test_messageChannel_sharedWorker.html new file mode 100644 index 0000000000..cf1864579f --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_sharedWorker.html @@ -0,0 +1,36 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - sharedWorker</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + var a = new SharedWorker('sharedWorker_messageChannel.js'); + a.port.onmessage = function(evt) { + is(evt.ports.length, 1, "We received a port."); + evt.ports[0].onmessage = function(e) { + is(e.data, 42, "Message reiceved back!"); + SimpleTest.finish(); + } + evt.ports[0].postMessage(42); + } + + SimpleTest.waitForExplicitFinish(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_sharedWorker2.html b/dom/messagechannel/tests/test_messageChannel_sharedWorker2.html new file mode 100644 index 0000000000..8b8f259308 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_sharedWorker2.html @@ -0,0 +1,34 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - sharedWorker</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> + <div id="content"></div> + + <script type="application/javascript"> + + var iframe = document.createElement('iframe'); + iframe.setAttribute('src', "iframe_messageChannel_sharedWorker2.html"); + document.getElementById('content').appendChild(iframe); + + var a = new SharedWorker('sharedWorker2_messageChannel.js'); + a.port.onmessage = function(evt) { + is(evt.ports.length, 1, "We received a port."); + evt.ports[0].onmessage = function(e) { + is(e.data, "Hello from the iframe!", "Message reiceved from the iframe!"); + SimpleTest.finish(); + } + } + + SimpleTest.waitForExplicitFinish(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_start.html b/dom/messagechannel/tests/test_messageChannel_start.html new file mode 100644 index 0000000000..4bc49cc0d6 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_start.html @@ -0,0 +1,235 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - start/close</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function runTests() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); + } + + function testOnMessage() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 2; + + a.port1.onmessage = function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + } + + a.port2.onmessage = function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + } + } + + function testAddEventListener() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + a.port1.addEventListener('message', function(evt) { + ok(false, "This method should not be called"); + }); + + a.port2.addEventListener('message', function(evt) { + ok(false, "This method should not be called"); + }); + + setTimeout(runTests, 0); + } + + function testAddEventListenerAndStart() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 2; + + a.port1.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + + a.port2.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + + a.port1.start(); + a.port2.start(); + } + + function testAddEventListener1AndStart() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 1; + + a.port1.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + + a.port2.addEventListener('message', function(evt) { + ok(false, "This method should not be called"); + }); + + a.port1.start(); + } + + function testAddEventListener2AndStart() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 1; + + a.port1.addEventListener('message', function(evt) { + ok(false, "This method should not be called"); + }); + + a.port2.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + + a.port2.start(); + } + + function testTimer() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + setTimeout(function() { + var events = 2; + a.port1.onmessage = function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + } + + a.port2.onmessage = function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + } + }, 200); + } + + function testAddEventListenerAndStartWrongOrder() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 2; + + a.port1.start(); + a.port1.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + + a.port2.start(); + a.port2.addEventListener('message', function(evt) { + ok(true, "This method should be called"); + if (!--events) runTests(); + }); + } + + function testOnMessageClone() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage(42); + a.port2.postMessage(43); + ok(true, "MessagePort{1,2}.postmessage() invoked"); + + var events = 2; + + addEventListener('message', testOnMessageCloneCb, false); + function testOnMessageCloneCb(event) { + a.port1.onmessage = function(evt) { + ok(true, "This method should be called"); + testOnMessageCloneFinish(); + } + + event.data.onmessage = function(evt) { + ok(true, "This method should be called"); + testOnMessageCloneFinish(); + } + + a.port2.onmessage = function(evt) { + ok(false, "This method should not be called"); + } + } + + function testOnMessageCloneFinish() { + if (!--events) { + removeEventListener('message', testOnMessageCloneCb); + runTests(); + } + } + + postMessage(a.port2, '*', [a.port2]); + } + + var tests = [ + testOnMessage, + testAddEventListener, + testAddEventListenerAndStart, + testAddEventListener1AndStart, + testAddEventListener2AndStart, + testTimer, + testAddEventListenerAndStartWrongOrder, + testOnMessageClone, + ]; + + SimpleTest.waitForExplicitFinish(); + SimpleTest.requestFlakyTimeout("untriaged"); + runTests(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_transferable.html b/dom/messagechannel/tests/test_messageChannel_transferable.html new file mode 100644 index 0000000000..44a27b34c4 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_transferable.html @@ -0,0 +1,111 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - port cloning</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function basic_test() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + if (evt.data.status == 'READY') { + a.port1.postMessage({ab, cb: ab}, [ab]); + ok(ab.byteLength == 0, "PostMessage - The size is: 0 == " + ab.byteLength) + } else { + ok(false, "Unknown message"); + } + } + + var div = document.getElementById("content"); + ok(div, "Parent exists"); + + var ifr = document.createElement("iframe"); + ifr.addEventListener("load", iframeLoaded); + ifr.setAttribute('src', "iframe_messageChannel_post.html"); + div.appendChild(ifr); + + function iframeLoaded() { + ifr.contentWindow.postMessage({ port: a.port2 }, '*', [a.port2]); + } + + a.port1.addEventListener('message', receivePortMessage); + function receivePortMessage(evt) { + is(evt.data.ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength); + window.removeEventListener('message', receiveMessage); + runTests(); + } + + // Start() is not implicity invoked when addEventListener is used. + a.port1.start(); + + var size = 1024 * 1024 * 32; + var ab = new ArrayBuffer(size); + is(ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength); + } + + function port_test() { + window.addEventListener('message', receiveMessage); + function receiveMessage(evt) { + ok(evt.data.type == 'OK', evt.data.msg); + } + + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var div = document.getElementById("content"); + ok(div, "Parent exists"); + + var ifr = document.createElement("iframe"); + ifr.addEventListener("load", iframeLoaded); + ifr.setAttribute('src', "iframe_messageChannel_transferable.html"); + div.appendChild(ifr); + + function iframeLoaded() { + ifr.contentWindow.postMessage('foobar!', '*', [a.port2]); + } + + a.port1.onmessage = function(evt) { + ok(evt.ports.length == 1, "Iframe sent a new port!"); + evt.ports[0].onmessage = function(event) { + is(event.data, "hello world!", "Message sent and received!"); + runTests(); + } + + evt.ports[0].postMessage("hello world!"); + } + } + + var tests = [ + basic_test, + port_test + ]; + + function runTests() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var t = tests.shift(); + t(); + } + + SimpleTest.waitForExplicitFinish(); + runTests(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_unshipped.html b/dom/messagechannel/tests/test_messageChannel_unshipped.html new file mode 100644 index 0000000000..44bf56979c --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_unshipped.html @@ -0,0 +1,123 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - unshipped message port queue</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + function test_orderedMessages() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var b = new MessageChannel(); + ok(b, "MessageChannel created"); + + var expectedNumber = 1; + function testEvent(number, id) { + is(expectedNumber, number, "This is the right number!"); + ok(!((expectedNumber - id) % 4), "From the right port: " + expectedNumber + " " + id); + expectedNumber++; + + if (expectedNumber >100) { + runTests(); + } + } + + a.port1.onmessage = function(evt) { + testEvent(evt.data, 2); + }; + + a.port2.onmessage = function(evt) { + testEvent(evt.data, 1); + }; + + b.port1.onmessage = function(evt) { + testEvent(evt.data, 4); + }; + + b.port2.onmessage = function(evt) { + testEvent(evt.data, 3); + }; + + for (var i = 0; i < 100;) { + a.port1.postMessage(++i); + a.port2.postMessage(++i); + b.port1.postMessage(++i); + b.port2.postMessage(++i); + } + } + + function test_unstarted() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var b = new MessageChannel(); + ok(b, "MessageChannel created"); + + var expectedNumber = 1; + function testEvent(number, id) { + is(expectedNumber, number, "This is the right number!"); + ok(!((expectedNumber - id) % 3), "From the right port: " + expectedNumber + " " + id); + expectedNumber++; + + // 102 because it's the first multiple of 3. + if (expectedNumber > 102) { + runTests(); + } + } + + a.port1.onmessage = function(evt) { + testEvent(evt.data, 2); + }; + + a.port2.onmessage = function(evt) { + testEvent(evt.data, 1); + }; + + b.port1.addEventListener("message", function() { + ok(false, "shouldn't be called"); + }); + + b.port2.onmessage = function(evt) { + testEvent(evt.data, 3); + }; + + for (var i = 0; i < 100;) { + a.port1.postMessage(++i); + a.port2.postMessage(++i); + b.port1.postMessage(++i); + b.port2.postMessage(1000); + } + } + + var tests = [ + test_orderedMessages, + test_unstarted + ]; + + function runTests() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); + } + + SimpleTest.waitForExplicitFinish(); + runTests(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_worker.html b/dom/messagechannel/tests/test_messageChannel_worker.html new file mode 100644 index 0000000000..742212a5d3 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_worker.html @@ -0,0 +1,60 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=677638 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 677638 - basic support</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + var tests = [ 0, 3 ]; + + function runTests() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var a = new Worker('worker_messageChannel.js'); + a.onmessage = function(evt) { + if (evt.data.type == 'finish') { + runTests(); + } else if (evt.data.type == 'info') { + info(evt.data.message); + } else if (evt.data.type == 'check') { + ok(evt.data.check, evt.data.message); + } else if (evt.data.type == 'port') { + is(evt.ports.length, 1, "A port has been received!"); + evt.ports[0].onmessage = function(e) { + e.target.postMessage(e.data); + } + } else if (evt.data.type == 'newport') { + var ch = new MessageChannel(); + ok(ch, "MessageChannel created"); + ch.port1.postMessage(42); + a.postMessage('a gift!', [ch.port2]); + } + } + + a.postMessage(tests.shift()); + } + + SimpleTest.waitForExplicitFinish(); + runTests(); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_messageChannel_worker_forceClose.html b/dom/messagechannel/tests/test_messageChannel_worker_forceClose.html new file mode 100644 index 0000000000..8de3450fb5 --- /dev/null +++ b/dom/messagechannel/tests/test_messageChannel_worker_forceClose.html @@ -0,0 +1,27 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for forcing the closing of the port in workers</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<div id="content"></div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + var worker = new Worker('data:javascript,onmessage = function(e) { "doing nothing with this port"; }'); + + var mc = new MessageChannel(); + worker.postMessage(42, [mc.port2]); + + for (var i = 0; i < 10; ++i) { + mc.port1.postMessage(i); + } + + ok(true, "All the messages are sent! We should shutdown correctly."); + </script> +</body> +</html> diff --git a/dom/messagechannel/tests/test_removedWindow.html b/dom/messagechannel/tests/test_removedWindow.html new file mode 100644 index 0000000000..b660000fd8 --- /dev/null +++ b/dom/messagechannel/tests/test_removedWindow.html @@ -0,0 +1,55 @@ +<!DOCTYPE html> +<title>MessagePort should not work when created from a disconnected window</title> +<script src="/tests/SimpleTest/SimpleTest.js"></script> +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> + +<body> +<script> +"use strict"; + +SimpleTest.waitForExplicitFinish(); +runTest(); + +async function runTest() { + let ifr = document.createElement('iframe'); + await new Promise(resolve => { + ifr.onload = resolve; + ifr.src = 'support/empty.html'; + document.body.appendChild(ifr); + }); + + let w = ifr.contentWindow; + + let pre = new w.MessageChannel(); + ok(!!pre, "We have a channel"); + + ifr.remove(); + + let post = new w.MessageChannel(); + ok(!!post, "We have a channel"); + + // This should silently fail. + pre.port1.postMessage(42); + pre.port2.onmessage = e => { + ok(false, "No messages should be received!"); + } + + // This should silently fail. + post.port1.postMessage(42); + post.port2.onmessage = e => { + ok(false, "No messages should be received!"); + } + + // Let's use another MessagePort just to be sure no messages are received by + // port2. + + let mc = new MessageChannel(); + mc.port1.postMessage(42); + mc.port2.onmessage = e => { + ok(true, "Ready to complete the test"); + SimpleTest.finish(); + } +} + +</script> +</body> diff --git a/dom/messagechannel/tests/unit/chromeWorker_messageChannel.js b/dom/messagechannel/tests/unit/chromeWorker_messageChannel.js new file mode 100644 index 0000000000..9ba92d3508 --- /dev/null +++ b/dom/messagechannel/tests/unit/chromeWorker_messageChannel.js @@ -0,0 +1,14 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +onmessage = function(pingEvt) { + if (pingEvt.data == "ping") { + let { port1, port2 } = new MessageChannel(); + port2.onmessage = helloEvt => { + if (helloEvt.data == "hello") { + helloEvt.ports[0].postMessage("goodbye"); + } + }; + pingEvt.ports[0].postMessage("pong", [port1]); + } +}; diff --git a/dom/messagechannel/tests/unit/test_messageChannel.js b/dom/messagechannel/tests/unit/test_messageChannel.js new file mode 100644 index 0000000000..d672920f71 --- /dev/null +++ b/dom/messagechannel/tests/unit/test_messageChannel.js @@ -0,0 +1,23 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +add_test(function test_messageChannel() { + do_test_pending(); + + let chromeWorker = new ChromeWorker( + "resource://test/chromeWorker_messageChannel.js" + ); + let { port1, port2 } = new MessageChannel(); + port2.onmessage = pongEvt => { + Assert.equal(pongEvt.data, "pong"); + let { port1: newPort1, port2: newPort2 } = new MessageChannel(); + newPort2.onmessage = goodbyeEvt => { + Assert.equal(goodbyeEvt.data, "goodbye"); + + do_test_finished(); + run_next_test(); + }; + pongEvt.ports[0].postMessage("hello", [newPort1]); + }; + chromeWorker.postMessage("ping", [port1]); +}); diff --git a/dom/messagechannel/tests/unit/xpcshell.ini b/dom/messagechannel/tests/unit/xpcshell.ini new file mode 100644 index 0000000000..7164d1ea96 --- /dev/null +++ b/dom/messagechannel/tests/unit/xpcshell.ini @@ -0,0 +1,7 @@ +[DEFAULT] +head = +skip-if = toolkit == 'android' +support-files = + chromeWorker_messageChannel.js + +[test_messageChannel.js] diff --git a/dom/messagechannel/tests/worker_messageChannel.js b/dom/messagechannel/tests/worker_messageChannel.js new file mode 100644 index 0000000000..83f65c3266 --- /dev/null +++ b/dom/messagechannel/tests/worker_messageChannel.js @@ -0,0 +1,110 @@ +function ok(a, msg) { + postMessage({ type: "check", check: !!a, message: msg }); +} + +function is(a, b, msg) { + ok(a === b, msg); +} + +function info(msg) { + postMessage({ type: "info", message: msg }); +} + +function finish() { + postMessage({ type: "finish" }); +} + +function basic() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + var port1 = a.port1; + ok(port1, "MessageChannel.port1 exists"); + is(port1, a.port1, "MessageChannel.port1 is port1"); + + var port2 = a.port2; + ok(port2, "MessageChannel.port1 exists"); + is(port2, a.port2, "MessageChannel.port2 is port2"); + + ["postMessage", "start", "close"].forEach(function(e) { + ok(e in port1, "MessagePort1." + e + " exists"); + ok(e in port2, "MessagePort2." + e + " exists"); + }); + + runTests(); +} + +function sendMessages() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage("Hello world!"); + a.port1.onmessage = function(e) { + is(e.data, "Hello world!", "The message is back!"); + runTests(); + }; + + a.port2.onmessage = function(e) { + a.port2.postMessage(e.data); + }; +} + +function transferPort() { + var a = new MessageChannel(); + ok(a, "MessageChannel created"); + + a.port1.postMessage("Hello world!"); + a.port1.onmessage = function(e) { + is(e.data, "Hello world!", "The message is back!"); + runTests(); + }; + + postMessage({ type: "port" }, [a.port2]); +} + +function transferPort2() { + onmessage = function(evt) { + is(evt.ports.length, 1, "A port has been received by the worker"); + evt.ports[0].onmessage = function(e) { + is(e.data, 42, "Data is 42!"); + runTests(); + }; + }; + + postMessage({ type: "newport" }); +} + +var tests = [basic, sendMessages, transferPort, transferPort2]; + +function runTests() { + if (!tests.length) { + finish(); + return; + } + + var t = tests.shift(); + t(); +} + +var subworker; +onmessage = function(evt) { + if (evt.data == 0) { + runTests(); + return; + } + + if (!subworker) { + info("Create a subworkers. ID: " + evt.data); + subworker = new Worker("worker_messageChannel.js"); + subworker.onmessage = function(e) { + info("Proxy a message to the parent."); + postMessage(e.data, e.ports); + }; + + subworker.postMessage(evt.data - 1); + return; + } + + info("Dispatch a message to the subworker."); + subworker.postMessage(evt.data, evt.ports); +}; diff --git a/dom/messagechannel/tests/worker_messageChannel_any.js b/dom/messagechannel/tests/worker_messageChannel_any.js new file mode 100644 index 0000000000..0c45680748 --- /dev/null +++ b/dom/messagechannel/tests/worker_messageChannel_any.js @@ -0,0 +1,7 @@ +onmessage = function(evt) { + evt.data.onmessage = function(event) { + evt.data.postMessage(event.data); + }; +}; + +postMessage("READY"); |