diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /testing/web-platform/tests/webmessaging/message-channels | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/webmessaging/message-channels')
12 files changed, 262 insertions, 0 deletions
diff --git a/testing/web-platform/tests/webmessaging/message-channels/basics.any.js b/testing/web-platform/tests/webmessaging/message-channels/basics.any.js new file mode 100644 index 0000000000..5732fb268a --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/basics.any.js @@ -0,0 +1,12 @@ +// META: title=basic messagechannel test + +async_test(function(t) { + var channel = new MessageChannel(); + channel.port1.postMessage(1); + channel.port2.onmessage = t.step_func( + function(e) { + assert_equals(e.data, 1); + t.done(); + }); + channel.port2.start(); +}); diff --git a/testing/web-platform/tests/webmessaging/message-channels/close.any.js b/testing/web-platform/tests/webmessaging/message-channels/close.any.js new file mode 100644 index 0000000000..8741d894b9 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/close.any.js @@ -0,0 +1,62 @@ +// How long (in ms) these tests should wait before deciding no further messages +// will be received. +const time_to_wait_for_messages = 100; + +async_test(t => { + const c = new MessageChannel(); + c.port1.onmessage = t.unreached_func('Should not have delivered message'); + c.port1.close(); + c.port2.postMessage('TEST'); + setTimeout(t.step_func_done(), time_to_wait_for_messages); + }, 'Message sent to closed port should not arrive.'); + +async_test(t => { + const c = new MessageChannel(); + c.port1.onmessage = t.unreached_func('Should not have delivered message'); + c.port2.close(); + c.port2.postMessage('TEST'); + setTimeout(t.step_func_done(), time_to_wait_for_messages); + }, 'Message sent from closed port should not arrive.'); + +async_test(t => { + const c = new MessageChannel(); + c.port1.onmessage = t.unreached_func('Should not have delivered message'); + c.port1.close(); + const c2 = new MessageChannel(); + c2.port1.onmessage = t.step_func(e => { + e.ports[0].postMessage('TESTMSG'); + setTimeout(t.step_func_done(), time_to_wait_for_messages); + }); + c2.port2.postMessage('TEST', [c.port2]); + }, 'Message sent to closed port from transferred port should not arrive.'); + +async_test(t => { + const c = new MessageChannel(); + let isClosed = false; + c.port1.onmessage = t.step_func_done(e => { + assert_true(isClosed); + assert_equals(e.data, 'TEST'); + }); + c.port2.postMessage('TEST'); + c.port2.close(); + isClosed = true; + }, 'Inflight messages should be delivered even when sending port is closed afterwards.'); + +async_test(t => { + const c = new MessageChannel(); + c.port1.onmessage = t.step_func_done(e => { + if (e.data == 'DONE') t.done(); + assert_equals(e.data, 'TEST'); + c.port1.close(); + }); + c.port2.postMessage('TEST'); + c.port2.postMessage('DONE'); + }, 'Close in onmessage should not cancel inflight messages.'); + +test(() => { + const c1 = new MessageChannel(); + const c2 = new MessageChannel(); + c1.port1.close(); + assert_throws_dom("DataCloneError", () => c2.port1.postMessage(null, [c1.port1])); + c2.port1.postMessage(null, [c1.port2]); +}, "close() detaches a MessagePort (but not the one its entangled with)"); diff --git a/testing/web-platform/tests/webmessaging/message-channels/cross-document.html b/testing/web-platform/tests/webmessaging/message-channels/cross-document.html new file mode 100644 index 0000000000..f4512ba5a2 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/cross-document.html @@ -0,0 +1,22 @@ +<!doctype html> +<title>cross-document channel</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<iframe src=resources/cross-document-1.html></iframe><iframe src=resources/cross-document-2.html></iframe> +<div id="log"></div> +<script> +var t = async_test(); +onload = t.step_func( + function() { + var channel = new MessageChannel(); + window[0].postMessage(1, '*', [channel.port1]); + window[1].postMessage(2, '*', [channel.port2]); + channel = null; + window.onmessage = t.step_func( + function(e) { + assert_equals(e.data, 1); + t.done(); + }); + } +); +</script> diff --git a/testing/web-platform/tests/webmessaging/message-channels/detached-iframe.window.js b/testing/web-platform/tests/webmessaging/message-channels/detached-iframe.window.js new file mode 100644 index 0000000000..c19f50ff93 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/detached-iframe.window.js @@ -0,0 +1,46 @@ +// META: title=MessageChannel in a detached iframe test +// META: script=/service-workers/service-worker/resources/test-helpers.sub.js +// META: script=/common/gc.js +// Pull in the with_iframe helper function from the service worker tests + + +const IframeAction = { + REMOVE_BEFORE_CREATION: 'remove-before-creation', + REMOVE_AFTER_CREATION: 'remove-after-creation', +}; + +async function detached_frame_test(t, action) { + const iframe = await with_iframe('about:blank'); + const iframe_MessageChannel = iframe.contentWindow.MessageChannel; + + if (action === IframeAction.REMOVE_BEFORE_CREATION) { + iframe.remove(); + } + + (() => { + const mc = new iframe_MessageChannel(); + mc.port1.postMessage("boo"); + mc.port2.onmessage = t.unreached_func("message event received"); + mc.port2.onmessageerror = t.unreached_func("message event received"); + })(); + + if (action === IframeAction.REMOVE_AFTER_CREATION) { + iframe.remove(); + } + + await garbageCollect(); + + // We are testing that neither of the above two events fire. We assume that a 2 second timeout + // is good enough. We can't use any other API for an end condition because each MessagePort has + // its own independent port message queue, which has no ordering guarantees relative to other + // APIs. + await new Promise(resolve => t.step_timeout(resolve, 2000)); +} + +promise_test(async (t) => { + return detached_frame_test(t, IframeAction.REMOVE_AFTER_CREATION); +}, 'MessageChannel created from a detached iframe should not send messages (remove after create)'); + +promise_test(async (t) => { + return detached_frame_test(t, IframeAction.REMOVE_BEFORE_CREATION); +}, 'MessageChannel created from a detached iframe should not send messages (remove before create)'); diff --git a/testing/web-platform/tests/webmessaging/message-channels/dictionary-transferrable.any.js b/testing/web-platform/tests/webmessaging/message-channels/dictionary-transferrable.any.js new file mode 100644 index 0000000000..bf49fddb99 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/dictionary-transferrable.any.js @@ -0,0 +1,13 @@ +// META: title=basic messagechannel with transfer + +async_test(function(t) { + var channel = new MessageChannel(); + var ab = new ArrayBuffer(1); + channel.port1.postMessage(ab, {transfer: [ab]}); + channel.port2.onmessage = t.step_func( + function(e) { + assert_equals(e.data.byteLength, 1); + t.done(); + }); + channel.port2.start(); +}); diff --git a/testing/web-platform/tests/webmessaging/message-channels/implied-start.any.js b/testing/web-platform/tests/webmessaging/message-channels/implied-start.any.js new file mode 100644 index 0000000000..460d26b7e4 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/implied-start.any.js @@ -0,0 +1,14 @@ +// META: title=onmessage implied start() + +async_test(function(t) { + var channel = new MessageChannel(); + channel.port1.postMessage(1); + channel.port2.onmessage = function() { + setTimeout(t.step_func(function() { + t.done(); + }), 50); + channel.port2.onmessage = t.step_func(function() { + assert_unreached(); + }); + }; // implies start() +}); diff --git a/testing/web-platform/tests/webmessaging/message-channels/no-start.any.js b/testing/web-platform/tests/webmessaging/message-channels/no-start.any.js new file mode 100644 index 0000000000..75b1ea1b26 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/no-start.any.js @@ -0,0 +1,9 @@ +// META: title=without start() + +async_test(function(t) { + var channel = new MessageChannel(); + channel.port1.postMessage(1); + var i = 0; + channel.port2.addEventListener('message', function() { i++; }, false); + setTimeout(t.step_func(function() { assert_equals(i, 0); t.done();}), 50); +}); diff --git a/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-1.html b/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-1.html new file mode 100644 index 0000000000..93725a93f8 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-1.html @@ -0,0 +1,8 @@ +<!doctype html> +<title>crosss-document-1</title> +<script> +onmessage = function(e) { + var port = e.ports[0]; + port.postMessage(e.data); +} +</script>
\ No newline at end of file diff --git a/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-2.html b/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-2.html new file mode 100644 index 0000000000..1b8ef0a7a0 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-2.html @@ -0,0 +1,10 @@ +<!doctype html> +<title>004-2</title> +<script> +onmessage = function(e) { + var port = e.ports[0]; + port.onmessage = function(e) { // implied start() + parent.postMessage(e.data, '*'); + } +} +</script>
\ No newline at end of file diff --git a/testing/web-platform/tests/webmessaging/message-channels/user-activation.tentative.any.js b/testing/web-platform/tests/webmessaging/message-channels/user-activation.tentative.any.js new file mode 100644 index 0000000000..175662873c --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/user-activation.tentative.any.js @@ -0,0 +1,21 @@ +// META: title=user activation messagechannel test + +async_test(function(t) { + var channel = new MessageChannel(); + channel.port1.postMessage(1, {includeUserActivation: true}); + channel.port1.postMessage(2); + var expected_data = 1; + channel.port2.onmessage = t.step_func( + function(e) { + assert_equals(e.data, expected_data); + expected_data++; + if (e.data == 1) { + assert_false(e.userActivation.isActive); + assert_false(e.userActivation.hasBeenActive); + } else { + assert_equals(e.userActivation, null); + t.done(); + } + }); + channel.port2.start(); +}); diff --git a/testing/web-platform/tests/webmessaging/message-channels/worker-post-after-close.any.js b/testing/web-platform/tests/webmessaging/message-channels/worker-post-after-close.any.js new file mode 100644 index 0000000000..2de0c434de --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/worker-post-after-close.any.js @@ -0,0 +1,28 @@ +async_test(t => { + function workerCode() { + onmessage = function(e) { + close(); + var mc = new MessageChannel(); + mc.port1.onmessage = function() { + postMessage("message received!"); + } + mc.port2.postMessage(42); + postMessage("done"); + } + } + + var workerBlob = new Blob([workerCode.toString() + ";workerCode();"], {type:"application/javascript"}); + + var w = new Worker(URL.createObjectURL(workerBlob)); + w.postMessage(''); + w.onmessage = function(e) { + if (e.data == "done") { + setTimeout(function() { + t.done(); + }, 250); + } else { + assert_true(false, "A wrong message has been received!"); + } + } +}, 'MessageChannel/MessagePort should not work after a worker self.close()'); + diff --git a/testing/web-platform/tests/webmessaging/message-channels/worker.any.js b/testing/web-platform/tests/webmessaging/message-channels/worker.any.js new file mode 100644 index 0000000000..633d89a371 --- /dev/null +++ b/testing/web-platform/tests/webmessaging/message-channels/worker.any.js @@ -0,0 +1,17 @@ +async_test(t => { + function workerCode() { + close(); + var mc = new MessageChannel(); + mc.port1.postMessage(42); + mc.port2.postMessage(42); + postMessage(true); + } + + var workerBlob = new Blob([workerCode.toString() + ";workerCode();"], {type:"application/javascript"}); + + var w = new Worker(URL.createObjectURL(workerBlob)); + w.onmessage = function(e) { + assert_true(e.data, "MessageChannel created on worker shutdown."); + t.done(); + } +}, 'MessageChannel/MessagePort created and used after a worker self.close()'); |