Bug 1565453 [wpt PR 17663] - Continue message port, a=testonly

Automatic update from web-platform-tests
continue messageport, transferable, postmessage options

--

wpt-commits: 60887c32e163723dde3e770d184215918880e432
wpt-pr: 17663
This commit is contained in:
Gregory Terzian 2019-10-22 17:17:51 +00:00 committed by James Graham
parent 57df9a508b
commit 80e9f9f851
5 changed files with 201 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// META: script=/common/get-host-info.sub.js
async_test(function(t) {
var channel1 = new MessageChannel();
var host = get_host_info();
let iframe = document.createElement('iframe');
iframe.src = host.HTTP_NOTSAMESITE_ORIGIN + "/webmessaging/support/ChildWindowPostMessage.htm";
document.body.appendChild(iframe);
var TARGET = document.querySelector("iframe").contentWindow;
iframe.onload = t.step_func(function() {
// Enable the port.
channel1.port1.onmessage = t.step_func(function (evt) {
assert_equals(Number(evt.data), 0);
// Send a message, expecting it to be received in the iframe.
channel1.port2.postMessage(1)
// Transfer the port.
TARGET.postMessage("ports", "*", [channel1.port1]);
});
// Send a message, expecting it to be received here.
channel1.port2.postMessage(0)
channel1.port2.onmessage = t.step_func(function (evt) {
assert_equals(Number(evt.data), 1);
t.done();
});
});
}, `Tasks enqueued on the port-message-queue of an enabled port,
are transferred along with the port, when the transfer happens in the same task
during which postMessage is called`);

View File

@ -0,0 +1,36 @@
async_test(function(t) {
var channel1 = new MessageChannel();
var channel2 = new MessageChannel();
// One, send a message.
channel1.port1.postMessage(1);
// Two, transfer both ports.
channel2.port1.postMessage("transfer", [channel1.port1]);
channel2.port1.postMessage("transfer", [channel1.port2]);
var transfer_counter = 0;
var sender;
channel2.port2.onmessage = t.step_func(function (evt) {
if (transfer_counter == 0) {
sender = evt.ports[0];
transfer_counter = 1;
} else {
sender.postMessage(2);
var counter = 0;
evt.ports[0].onmessage = t.step_func(function (evt) {
if (counter == 0) {
assert_equals(evt.data, 1);
counter = 1;
} else if (counter == 1) {
assert_equals(evt.data, 2);
counter = 2;
} else {
assert_equals(evt.data, 3);
t.done();
}
});
sender.postMessage(3);
}
});
}, "An entangled port transferred to the same origin receives messages in order");

View File

@ -0,0 +1,32 @@
async_test(function(t) {
var channel1 = new MessageChannel();
var channel2 = new MessageChannel();
var channel3 = new MessageChannel();
channel2.port2.onmessage = t.step_func(function (evt) {
channel3.port1.onmessage = t.step_func(function (evt) {
var counter = 0;
evt.ports[0].onmessage = t.step_func(function (evt) {
if (counter == 0) {
assert_equals(evt.data, "First");
counter = 1;
} else if (counter == 1) {
assert_equals(evt.data, "Second");
counter = 2;
} else if (counter == 2) {
assert_equals(evt.data, "Third");
counter = 3;
} else if (counter == 3) {
assert_equals(evt.data, "Fourth");
t.done();
}
});
channel1.port2.postMessage("Fourth");
});
channel1.port2.postMessage("Second");
channel1.port2.postMessage("Third");
channel3.port2.postMessage("2", evt.ports);
});
channel1.port2.postMessage("First");
channel2.port1.postMessage("1", [channel1.port1]);
}, `When transferring a non-enabled port mutiple times,
incoming messages sent at various transfer steps are received in order upon enablement.`);

View File

@ -0,0 +1,35 @@
async_test(function(t) {
var channel1 = new MessageChannel();
var channel2 = new MessageChannel();
var channel3 = new MessageChannel();
channel2.port2.onmessage = t.step_func(function (evt) {
evt.ports[0].postMessage("Second");
evt.ports[0].postMessage("Third");
channel3.port1.onmessage = t.step_func(function (evt) {
evt.ports[0].postMessage("Fourth");
});
channel3.port2.postMessage("2", evt.ports);
});
channel1.port1.postMessage("First");
channel2.port1.postMessage("1", [channel1.port1]);
var counter = 0;
channel1.port2.onmessage = t.step_func(function (evt) {
if (counter == 0) {
assert_equals(evt.data, "First");
counter = 1;
} else if (counter == 1) {
assert_equals(evt.data, "Second");
counter = 2;
}
else if (counter == 2) {
assert_equals(evt.data, "Third");
counter = 3;
}
else if (counter == 3) {
assert_equals(evt.data, "Fourth");
t.done();
}
});
}, `When transferring a port,
outgoing messages sent at each transfer step are received in order by the entangled port.`);

View File

@ -0,0 +1,66 @@
// META: script=/common/get-host-info.sub.js
async_test(function(t) {
var host = get_host_info();
var noteSameSiteURL = host.HTTP_NOTSAMESITE_ORIGIN + "/webmessaging/support/ChildWindowPostMessage.htm";
var TOTALPORTS = 100;
var LocalPorts = [];
var RemotePorts = [];
var PassedResult = 0;
var sum = 0;
let iframe = document.createElement('iframe');
iframe.src = noteSameSiteURL;
document.body.appendChild(iframe);
var TARGET = document.querySelector("iframe").contentWindow;
iframe.onload = t.step_func(function() {
assert_own_property(window, "MessageChannel", "window");
var channels = [];
for (var i=0; i<TOTALPORTS; i++)
{
channels[i] = new MessageChannel();
LocalPorts[i] = channels[i].port1;
LocalPorts[i].foo = i;
RemotePorts[i] = channels[i].port2;
LocalPorts[i].onmessage = t.step_func(function(e)
{
assert_equals(e.target.foo, e.data);
PassedResult++;
sum += e.data;
if (PassedResult == TOTALPORTS)
{
assert_equals(sum, 4950);
t.done();
}
});
}
// Sending in two batches, to test the two postMessage variants.
var firstBatch = RemotePorts.slice(0, 50);
var secondBatch = RemotePorts.slice(50, 100);
TARGET.postMessage("ports", "*", firstBatch);
TARGET.postMessage("ports", {targetOrigin: '*', transfer: secondBatch});
});
var counter = 0;
window.onmessage = function(e)
{
if (e.data === "ports")
{
if (counter == 0) {
for (var i=0; i<51; i++)
{
LocalPorts[i].postMessage(LocalPorts[i].foo);
}
counter = 1;
} else {
for (var i=51; i<100; i++)
{
LocalPorts[i].postMessage(LocalPorts[i].foo);
}
}
}
}
}, "Test Description: postMessage to cross-site iframe with MessagePort array containing 100 ports.");