Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
180 lines (159 sloc) 6.97 KB
<!DOCTYPE html>
<title>Test postMessage on PortalHost</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
function createPortal(portalSrc) {
var portal = document.createElement("portal");
portal.src = portalSrc;
return new Promise((resolve, reject) => {
portal.onload = () => {
resolve(portal);
};
document.body.appendChild(portal);
});
}
async function createPortalAndLoopMessage(portalSrc, params) {
var portal = await createPortal(portalSrc);
var waitForResponse = new Promise((resolve, reject) => {
portal.addEventListener("message", e => { resolve(e); });
});
portal.postMessage(params, "*");
return waitForResponse;
}
const crossOriginUrl = "http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/resources/portal-host-post-message.sub.html";
promise_test(async () => {
var {data, origin} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html", ["test", "*"]);
assert_equals(data, "test");
assert_equals(origin, "http://{{host}}:{{ports[http][0]}}");
}, "Message received after postMessage from portal host");
promise_test(async () => {
var {data, origin} = await createPortalAndLoopMessage(
crossOriginUrl, ["test", "*"]);
assert_equals(data, "test");
assert_equals(origin, "http://{{hosts[alt][www]}}:{{ports[http][0]}}");
}, "Message received after postMessage from portal host in cross-origin-portal");
promise_test(async () => {
var {data, origin} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html", ["test"]);
assert_equals(data, "test");
assert_equals(origin, "http://{{host}}:{{ports[http][0]}}");
}, "Message received from same-origin portal host with no target origin specified");
promise_test(async () => {
var {data, origin} = await createPortalAndLoopMessage(
crossOriginUrl, ["test", "http://{{host}}:{{ports[http][0]}}"]);
assert_equals(data, "test");
}, "Message received from cross-origin portal host with target origin correctly specified");
promise_test(async () => {
var message = {
prop1: "value1",
prop2: 2.5,
prop3: [1, 2, "3"],
prop4: {
prop4_1: "value4_1"
}
};
var {data} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html", [message]);
assert_object_equals(data, message);
var {data} = await createPortalAndLoopMessage(crossOriginUrl,
[message, "*"]);
assert_object_equals(data, message);
}, "postMessage with object message");
promise_test(async () => {
function checkPort(port) {
return new Promise((resolve, reject) => {
var channel = new MessageChannel();
channel.port1.onmessage = resolve;
port.postMessage("sending port", [channel.port2]);
});
}
var {ports} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html",
{type: "message-port"});
await checkPort(ports[0]);
var {ports} = await createPortalAndLoopMessage(
crossOriginUrl,
{type: "message-port"});
await checkPort(ports[0]);
}, "postMessage with message ports");
promise_test(async () => {
var {data} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html", {
type: "array-buffer-without-transfer",
array: [0, 1, 2, 3, 4]
});
assert_array_equals([0, 1, 2, 3, 4], new Int8Array(data.arrayBuffer));
var {data} = await createPortalAndLoopMessage(crossOriginUrl, {
type: "array-buffer-without-transfer",
array: [0, 1, 2, 3, 4]
});
assert_array_equals([0, 1, 2, 3, 4], new Int8Array(data.arrayBuffer));
}, "postMessage with array buffer without transfer");
promise_test(async () => {
var {data} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html", {
type: "array-buffer-with-transfer",
array: [0, 1, 2, 3, 4]
});
assert_array_equals([0, 1, 2, 3, 4], new Int8Array(data.arrayBuffer));
var {data} = await createPortalAndLoopMessage(crossOriginUrl, {
type: "array-buffer-with-transfer",
array: [0, 1, 2, 3, 4]
});
assert_array_equals([0, 1, 2, 3, 4], new Int8Array(data.arrayBuffer));
}, "postMessage with array buffer with transfer");
promise_test(async () => {
var {data} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html",
{type: "invalid-message"});
assert_equals(data.errorType, "DataCloneError");
}, "postMessage should throw error when serialization fails");
promise_test(async () => {
var {data} = await createPortalAndLoopMessage(
"resources/portal-host-post-message.sub.html",
{type: "invalid-port"});
assert_equals(data.errorType, "TypeError");
}, "postMessage with invalid transferable should throw error");
promise_test(async () => {
var receiveMessage = new Promise((resolve, reject) => {
var bc = new BroadcastChannel("portal-host-post-message-after-activate");
bc.onmessage = e => { resolve(e); };
});
const portalUrl = encodeURIComponent(
"portal-host-post-message-after-activate.html");
window.open(`resources/portal-embed-and-activate.html?url=${portalUrl}`);
var message = await receiveMessage;
assert_equals(message.data, "InvalidStateError");
}, "Calling postMessage after receiving onactivate event should fail");
promise_test(() => {
var portal = document.createElement("portal");
portal.src = "resources/portal-host-post-message-navigate-1.html";
var count = 0;
var waitForMessages = new Promise((resolve, reject) => {
portal.addEventListener("message", e => {
count++;
if (count == 2)
resolve();
});
});
document.body.appendChild(portal);
return waitForMessages;
}, "postMessage before and after portal navigation should work");
async_test(t => {
createPortalAndLoopMessage("resources/portal-host-post-message.sub.html",
["test", "http://{{hosts[alt][www]}}:{{ports[http][0]}}"])
.then(t.step_func(() => { assert_unreached("message delivered"); }));
t.step_timeout(t.done, 2000);
}, "Message should not be received from portal host with target set to different origin");
async_test(t => {
createPortalAndLoopMessage(crossOriginUrl, ["test"]).then(t.step_func(() => {
assert_unreached("message delivered");
}));
t.step_timeout(t.done, 2000);
}, "Message should not be received cross origin-portal host with no target origin set");
</script>
</body>
You can’t perform that action at this time.