Skip to content

Commit

Permalink
BroadcastChannel: Add new WPTs for tentative detached iframe behavior
Browse files Browse the repository at this point in the history
This CL adds WPTs to test that BroadcastChannels in detached iframes
behave according to proposed changes to the specification. For more
details, see:

whatwg/html#7219

Bug: 1239274
Change-Id: Ia014e412a2dc8267aac57041672835fb90994d89
  • Loading branch information
recvfrom authored and chromium-wpt-export-bot committed Oct 18, 2021
1 parent a30e4f7 commit 8bf5864
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions webmessaging/broadcastchannel/detached-iframe.tentative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<!-- Pull in the with_iframe helper function from the service worker tests -->
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<body>
<script>

const TEST_IFRAME_CLASS_NAME = 'test-iframe';

promise_test(async t => {
await with_iframe('about:blank');
const iframe = document.getElementsByClassName(TEST_IFRAME_CLASS_NAME)[0];
const iframe_BroadcastChannel = iframe.contentWindow.BroadcastChannel;
iframe.remove();

return new Promise((resolve) => {
assert_throws_dom('InvalidStateError', () => new iframe_BroadcastChannel('create-from-detached-iframe'));
resolve();
});
}, "InvalidStateError is raised when trying to create a BroadcastChannel instance from a detached iframe context");

const events = [];
var bc1;
const DONE_MSG = 'done';

function DetachedIframeTestCheckForOneMessage(t) {
return new Promise((resolve) => {
bc1.onmessage = t.step_func(e => {
events.push(e);
if (e.data == DONE_MSG) {
assert_equals(events.length, 1);
resolve();
}
});
});
}

promise_test(async t => {
await with_iframe('about:blank');
const iframe = document.getElementsByClassName(TEST_IFRAME_CLASS_NAME)[0];
const iframe_BroadcastChannel = iframe.contentWindow.BroadcastChannel;

events.length = 0;

const CHANNEL_NAME = 'postMessage-from-detached-iframe';
bc1 = new BroadcastChannel(CHANNEL_NAME);
const bc2 = new BroadcastChannel(CHANNEL_NAME);
const iframe_bc = new iframe_BroadcastChannel(CHANNEL_NAME);
iframe.remove();

const testResultsPromise = DetachedIframeTestCheckForOneMessage(t);

iframe_bc.postMessage("test");
bc2.postMessage(DONE_MSG);

bc2.close();
iframe_bc.close();
t.add_cleanup(() => bc1.close());

return testResultsPromise;
}, "Messages from BroadcastChannel instances associated with detached iframes are not sent");

promise_test(async t => {
await with_iframe('about:blank');
const iframe = document.getElementsByClassName(TEST_IFRAME_CLASS_NAME)[0];
const iframe_BroadcastChannel = iframe.contentWindow.BroadcastChannel;

events.length = 0;

const CHANNEL_NAME = 'postMessage-to-detached-iframe';
// iframe_bc must be created first so that it receives messages before bc1
const iframe_bc = new iframe_BroadcastChannel(CHANNEL_NAME);
iframe_bc.onmessage = e => {events.push(e)};
bc1 = new BroadcastChannel(CHANNEL_NAME);
const bc2 = new BroadcastChannel(CHANNEL_NAME);
iframe.remove();

const testResultsPromise = DetachedIframeTestCheckForOneMessage(t);
bc2.postMessage(DONE_MSG);

bc2.close();
iframe_bc.close();
t.add_cleanup(() => bc1.close());

return testResultsPromise;
}, "Messages to BroadcastChannel instances associated with detached iframes are not received");


</script>
</body>

0 comments on commit 8bf5864

Please sign in to comment.