Commit 961ddc20 authored by Adithya Srinivasan's avatar Adithya Srinivasan Committed by Commit Bot

Portals: Simplify tests (5/5)

Uses portal postMessage APIs instead of broadcast channel in the
following tests:

- portals-activate-inside-portal.html
- portals-no-referrer.html

Change-Id: Iaa4d8a79fed99f7b39181eec59f26d89b7e38864
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1591804Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarLucas Gadani <lfg@chromium.org>
Commit-Queue: Adithya Srinivasan <adithyas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#657757}
parent 2b93b1fb
...@@ -7,11 +7,7 @@ ...@@ -7,11 +7,7 @@
var portal = document.createElement("portal"); var portal = document.createElement("portal");
portal.src = "resources/portal-activate-inside-portal.html"; portal.src = "resources/portal-activate-inside-portal.html";
let waitForMessage = new Promise((resolve, reject) => { let waitForMessage = new Promise((resolve, reject) => {
var bc = new BroadcastChannel("portals-activate-inside-portal"); portal.onmessage = e => resolve(e.data);
bc.onmessage = e => {
bc.close();
resolve(e.data);
}
document.body.appendChild(portal); document.body.appendChild(portal);
}); });
var error = await waitForMessage; var error = await waitForMessage;
......
...@@ -3,30 +3,20 @@ ...@@ -3,30 +3,20 @@
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<body> <body>
<script> <script>
// TODO(jbroman): Remove use of BroadcastChannel for messaging once it is
// possible to postMessage to a portal the normal way.
promise_test(async () => { promise_test(async () => {
assert_true('HTMLPortalElement' in self, 'HTMLPortalElement is required for this test'); assert_true('HTMLPortalElement' in self, 'HTMLPortalElement is required for this test');
let portal = document.createElement('portal'); let portal = document.createElement('portal');
let channelName = 'portals-no-referrer'; let referrerPromise = new Promise((resolve, reject) => {
let broadcastChannel = new BroadcastChannel(channelName); portal.addEventListener('message', e => resolve(e.data), {once: true});
});
portal.src = 'resources/postmessage-referrer.sub.html';
document.body.appendChild(portal);
try { try {
let referrerPromise = new Promise((resolve, reject) => { let {httpReferrer, documentReferrer} = await referrerPromise;
broadcastChannel.addEventListener('message', e => { assert_equals(httpReferrer, 'no-http-referrer', 'No HTTP Referer header should be sent');
resolve(e.data); assert_equals(documentReferrer, 'no-document-referrer', 'No document.referrer should be present');
}, {once: true});
});
portal.src = `resources/postmessage-referrer.sub.html?broadcastchannel=${channelName}`;
document.body.appendChild(portal);
try {
let {httpReferrer, documentReferrer} = await referrerPromise;
assert_equals(httpReferrer, 'no-http-referrer', 'No HTTP Referer header should be sent');
assert_equals(documentReferrer, 'no-document-referrer', 'No document.referrer should be present');
} finally {
document.body.removeChild(portal);
}
} finally { } finally {
broadcastChannel.close(); document.body.removeChild(portal);
} }
}, "portal contents should be loaded with no referrer"); }, "portal contents should be loaded with no referrer");
</script> </script>
......
...@@ -4,11 +4,7 @@ ...@@ -4,11 +4,7 @@
var portal = document.createElement("portal"); var portal = document.createElement("portal");
portal.src = "simple-portal.html"; portal.src = "simple-portal.html";
portal.onmessage = () => { portal.onmessage = () => {
portal.activate().catch(e => { portal.activate().catch(e => window.portalHost.postMessage(e.name, "*"));
var bc2 = new BroadcastChannel("portals-activate-inside-portal");
bc2.postMessage(e.name);
bc2.close();
});
} }
document.body.appendChild(portal); document.body.appendChild(portal);
</script> </script>
......
...@@ -12,9 +12,7 @@ ...@@ -12,9 +12,7 @@
} }
}); });
var bc = new BroadcastChannel("portal"); window.portalHost.postMessage("loaded", "*");
bc.postMessage("loaded");
bc.close();
Promise.all([postMessagePromise, activatePromise]) Promise.all([postMessagePromise, activatePromise])
.then(values => { .then(values => {
......
...@@ -5,11 +5,9 @@ ...@@ -5,11 +5,9 @@
portal.src = "portal-post-message-before-activate-portal.html"; portal.src = "portal-post-message-before-activate-portal.html";
document.body.appendChild(portal); document.body.appendChild(portal);
var bc = new BroadcastChannel("portal"); portal.onmessage = () => {
bc.onmessage = () => {
portal.postMessage("message"); portal.postMessage("message");
portal.activate(); portal.activate();
bc.close();
} }
</script> </script>
</body> </body>
<!DOCTYPE html> <!DOCTYPE html>
<script> <script>
let message = { let message = {
httpReferrer: '{{header_or_default(Referer, no-http-referrer)}}', httpReferrer: '{{header_or_default(Referer, no-http-referrer)}}',
documentReferrer: document.referrer || 'no-document-referrer', documentReferrer: document.referrer || 'no-document-referrer',
}; };
window.portalHost.postMessage(message, "*");
let broadcastChannel = new BroadcastChannel(new URL(location).searchParams.get('broadcastchannel'));
try {
broadcastChannel.postMessage(message);
} finally {
broadcastChannel.close();
}
</script> </script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment