summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webmessaging/message-channels
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/webmessaging/message-channels
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/webmessaging/message-channels')
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/basics.any.js12
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/close.any.js62
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/cross-document.html22
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/detached-iframe.window.js46
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/dictionary-transferrable.any.js13
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/implied-start.any.js14
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/no-start.any.js9
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-1.html8
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/resources/cross-document-2.html10
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/user-activation.tentative.any.js21
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/worker-post-after-close.any.js28
-rw-r--r--testing/web-platform/tests/webmessaging/message-channels/worker.any.js17
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()');