From d6faf548b2fb87b44f60d311b94ed780d4726245 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 5 Feb 2020 22:10:52 +0000 Subject: [PATCH] Bug 1611855 - Worklet must be part of the same parent's agentCluster - part 10 - split test_postMessages.html, r=smaug Differential Revision: https://phabricator.services.mozilla.com/D61503 --HG-- rename : dom/base/test/test_postMessages.html => dom/base/test/common_postMessages.js rename : dom/base/test/test_postMessages.html => dom/base/test/test_postMessages_broadcastChannel.html rename : dom/base/test/test_postMessages.html => dom/base/test/test_postMessages_messagePort.html rename : dom/base/test/test_postMessages.html => dom/base/test/test_postMessages_window.html rename : dom/base/test/test_postMessages.html => dom/base/test/test_postMessages_workers.html extra : moz-landing-system : lando --- dom/base/test/common_postMessages.js | 340 ++++++++ dom/base/test/mochitest.ini | 8 +- dom/base/test/test_postMessages.html | 775 ------------------ .../test_postMessages_broadcastChannel.html | 167 ++++ .../test/test_postMessages_messagePort.html | 114 +++ dom/base/test/test_postMessages_window.html | 124 +++ dom/base/test/test_postMessages_workers.html | 111 +++ 7 files changed, 862 insertions(+), 777 deletions(-) create mode 100644 dom/base/test/common_postMessages.js delete mode 100644 dom/base/test/test_postMessages.html create mode 100644 dom/base/test/test_postMessages_broadcastChannel.html create mode 100644 dom/base/test/test_postMessages_messagePort.html create mode 100644 dom/base/test/test_postMessages_window.html create mode 100644 dom/base/test/test_postMessages_workers.html diff --git a/dom/base/test/common_postMessages.js b/dom/base/test/common_postMessages.js new file mode 100644 index 000000000000..b40189a0d13d --- /dev/null +++ b/dom/base/test/common_postMessages.js @@ -0,0 +1,340 @@ +function setup_tests() { + SpecialPowers.pushPrefEnv({ set: [["dom.input.dirpicker", true]] }, next); +} + +function getType(a) { + if (a === null || a === undefined) { + return "null"; + } + + if (Array.isArray(a)) { + return "array"; + } + + if (typeof a == "object") { + return "object"; + } + + if ( + SpecialPowers.Cu.getJSTestingFunctions().wasmIsSupported() && + a instanceof WebAssembly.Module + ) { + return "wasm"; + } + + 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(), "Props names match"); + + for (var p in a) { + compare(a[p], b[p]); + } + + return; + } + + if (type == "wasm") { + var wasmA = new WebAssembly.Instance(a); + ok(wasmA instanceof WebAssembly.Instance, "got an instance"); + + var wasmB = new WebAssembly.Instance(b); + ok(wasmB instanceof WebAssembly.Instance, "got an instance"); + + ok(wasmA.exports.foo() === wasmB.exports.foo(), "Same result!"); + ok(wasmB.exports.foo() === 42, "We want 42"); + } + + if (type != "null") { + is(a, b, "Same value"); + } +} + +var clonableObjects = [ + { target: "all", data: "hello world" }, + { target: "all", data: 123 }, + { target: "all", data: null }, + { target: "all", data: true }, + { target: "all", data: new Date() }, + { target: "all", data: [1, "test", true, new Date()] }, + { + target: "all", + data: { a: true, b: null, c: new Date(), d: [true, false, {}] }, + }, + { target: "all", data: new Blob([123], { type: "plain/text" }) }, + { target: "all", data: new ImageData(2, 2) }, +]; + +function create_fileList() { + var url = SimpleTest.getTestFileURL("script_postmessages_fileList.js"); + var script = SpecialPowers.loadChromeScript(url); + + function onOpened(message) { + var fileList = document.getElementById("fileList"); + SpecialPowers.wrap(fileList).mozSetFileArray([message.file]); + + // Just a simple test + var domFile = fileList.files[0]; + is(domFile.name, "prefs.js", "fileName should be prefs.js"); + + clonableObjects.push({ target: "all", data: fileList.files }); + script.destroy(); + next(); + } + + script.addMessageListener("file.opened", onOpened); + script.sendAsyncMessage("file.open"); +} + +function create_directory() { + if (navigator.userAgent.toLowerCase().includes("Android")) { + next(); + return; + } + + var url = SimpleTest.getTestFileURL("script_postmessages_fileList.js"); + var script = SpecialPowers.loadChromeScript(url); + + function onOpened(message) { + var fileList = document.getElementById("fileList"); + SpecialPowers.wrap(fileList).mozSetDirectory(message.dir); + + fileList.getFilesAndDirectories().then(function(list) { + // Just a simple test + is(list.length, 1, "This list has 1 element"); + ok(list[0] instanceof Directory, "We have a directory."); + + clonableObjects.push({ target: "all", data: list[0] }); + script.destroy(); + next(); + }); + } + + script.addMessageListener("dir.opened", onOpened); + script.sendAsyncMessage("dir.open"); +} + +function create_wasmModule() { + info("Checking if we can play with WebAssembly..."); + + if (!SpecialPowers.Cu.getJSTestingFunctions().wasmIsSupported()) { + next(); + return; + } + + ok(WebAssembly, "WebAssembly object should exist"); + ok(WebAssembly.compile, "WebAssembly.compile function should exist"); + + const wasmTextToBinary = SpecialPowers.unwrap( + SpecialPowers.Cu.getJSTestingFunctions().wasmTextToBinary + ); + const fooModuleCode = wasmTextToBinary(`(module + (func $foo (result i32) (i32.const 42)) + (export "foo" $foo) + )`); + + WebAssembly.compile(fooModuleCode).then( + m => { + ok(m instanceof WebAssembly.Module, "The WasmModule has been compiled."); + clonableObjects.push({ target: "sameProcess", data: m }); + next(); + }, + () => { + ok(false, "The compilation of the wasmModule failed."); + } + ); +} + +function runTests(obj) { + ok( + "clonableObjectsEveryWhere" in obj && + "clonableObjectsSameProcess" in obj && + "transferableObjects" in obj && + (obj.clonableObjectsEveryWhere || + obj.clonableObjectsSameProcess || + obj.transferableObjects), + "We must run some test!" + ); + + // cloning tests - everyWhere + new Promise(function(resolve, reject) { + var clonableObjectsId = 0; + function runClonableTest() { + if (clonableObjectsId >= clonableObjects.length) { + resolve(); + return; + } + + var object = clonableObjects[clonableObjectsId++]; + + if (object.target != "all") { + runClonableTest(); + return; + } + + obj + .send(object.data, []) + .catch(() => { + return { error: true }; + }) + .then(received => { + if (!obj.clonableObjectsEveryWhere) { + ok(received.error, "Error expected"); + } else { + ok(!received.error, "Error not expected"); + compare(received.data, object.data); + } + runClonableTest(); + }); + } + + runClonableTest(); + }) + + // clonable same process + .then(function() { + return new Promise(function(resolve, reject) { + var clonableObjectsId = 0; + function runClonableTest() { + if (clonableObjectsId >= clonableObjects.length) { + resolve(); + return; + } + + var object = clonableObjects[clonableObjectsId++]; + + if (object.target != "sameProcess") { + runClonableTest(); + return; + } + + obj + .send(object.data, []) + .catch(() => { + return { error: true }; + }) + .then(received => { + if (!obj.clonableObjectsSameProcess) { + ok(received.error, "Error expected"); + } else { + ok(!received.error, "Error not expected"); + compare(received.data, object.data); + } + runClonableTest(); + }); + } + + runClonableTest(); + }); + }) + + // transfering tests + .then(function() { + if (!obj.transferableObjects) { + return; + } + + // MessagePort + return new Promise(function(r, rr) { + var mc = new MessageChannel(); + obj.send(42, [mc.port1]).then(function(received) { + is(received.ports.length, 1, "MessagePort has been transferred"); + mc.port2.postMessage("hello world"); + received.ports[0].onmessage = function(e) { + is(e.data, "hello world", "Ports are connected!"); + r(); + }; + }); + }); + }) + + // no dup transfering + .then(function() { + if (!obj.transferableObjects) { + return; + } + + // MessagePort + return new Promise(function(r, rr) { + var mc = new MessageChannel(); + obj + .send(42, [mc.port1, mc.port1]) + .then( + function(received) { + ok(false, "Duplicate ports should throw!"); + }, + function() { + ok(true, "Duplicate ports should throw!"); + } + ) + .then(r); + }); + }) + + // non transfering tests + .then(function() { + if (obj.transferableObjects) { + return; + } + + // MessagePort + return new Promise(function(r, rr) { + var mc = new MessageChannel(); + obj + .send(42, [mc.port1]) + .then( + function(received) { + ok(false, "This object should not support port transferring"); + }, + function() { + ok(true, "This object should not support port transferring"); + } + ) + .then(r); + }); + }) + + // done. + .then(function() { + obj.finished(); + }); +} + +function next() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); +} + +var tests = [setup_tests, create_fileList, create_directory, create_wasmModule]; diff --git a/dom/base/test/mochitest.ini b/dom/base/test/mochitest.ini index ad7f17757be7..50c459be365e 100644 --- a/dom/base/test/mochitest.ini +++ b/dom/base/test/mochitest.ini @@ -210,7 +210,9 @@ support-files = referrer_header.sjs referrer_header_current_document_iframe.html script_postmessages_fileList.js + common_postMessages.js iframe_postMessages.html + worker_postMessages.js test_anonymousContent_style_csp.html^headers^ file_explicit_user_agent.sjs referrer_change_server.sjs @@ -779,8 +781,10 @@ skip-if = toolkit == 'android' # Plugins don't work on Android tags = audiochannel skip-if = toolkit == 'android' # Plugins don't work on Android [test_postMessage_solidus.html] -[test_postMessages.html] -support-files = worker_postMessages.js +[test_postMessages_window.html] +[test_postMessages_workers.html] +[test_postMessages_broadcastChannel.html] +[test_postMessages_messagePort.html] [test_postMessage_originAttributes.html] support-files = file_receiveMessage.html [test_processing_instruction_update_stylesheet.xhtml] diff --git a/dom/base/test/test_postMessages.html b/dom/base/test/test_postMessages.html deleted file mode 100644 index 14bf12afe7be..000000000000 --- a/dom/base/test/test_postMessages.html +++ /dev/null @@ -1,775 +0,0 @@ - - - - Test for postMessages cloning/transferring objects - - - - - - - - - diff --git a/dom/base/test/test_postMessages_broadcastChannel.html b/dom/base/test/test_postMessages_broadcastChannel.html new file mode 100644 index 000000000000..a3864d0710ea --- /dev/null +++ b/dom/base/test/test_postMessages_broadcastChannel.html @@ -0,0 +1,167 @@ + + + + Test for postMessages cloning/transferring objects + + + + + + + + + + diff --git a/dom/base/test/test_postMessages_messagePort.html b/dom/base/test/test_postMessages_messagePort.html new file mode 100644 index 000000000000..5797c23c5c15 --- /dev/null +++ b/dom/base/test/test_postMessages_messagePort.html @@ -0,0 +1,114 @@ + + + + Test for postMessages cloning/transferring objects + + + + + + + + + + diff --git a/dom/base/test/test_postMessages_window.html b/dom/base/test/test_postMessages_window.html new file mode 100644 index 000000000000..12b757867b44 --- /dev/null +++ b/dom/base/test/test_postMessages_window.html @@ -0,0 +1,124 @@ + + + + Test for postMessages cloning/transferring objects + + + + + + + + + + diff --git a/dom/base/test/test_postMessages_workers.html b/dom/base/test/test_postMessages_workers.html new file mode 100644 index 000000000000..994c0201e623 --- /dev/null +++ b/dom/base/test/test_postMessages_workers.html @@ -0,0 +1,111 @@ + + + + Test for postMessages cloning/transferring objects + + + + + + + + + +