<!DOCTYPE html>
<!-- Tests UseCounter for service worker controlled page in third party context.
This cannot be upstreamed to WPT because it tests Chrome's UseCounter
mechanism. -->
<meta charset="utf-8">
<title>Test UseCounter for service worker controlled page in third party context.</title>
<body></body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/get-host-info.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
// Check that the iframe created in |win| has observed the use counter
// kThirdPartyServiceWorker.
function assert_subframe_has_use_counter(win, description) {
let saw_message = new Promise(resolve => {
window.onmessage = (e => resolve(e.data));
win.postMessage('from_opener_assert_has_use_counter', '*');
});
return saw_message.then(data => assert_equals(data, 'from_iframe_verified_has_use_counter', description));
}
function popup_page_loaded() {
return new Promise(function(resolve, reject) {
window.onmessage = (msg) => {
if (msg.data == 'popup_page_loaded')
resolve();
else
reject(msg.data);
};
});
}
// This test opens a cross-origin popup and then the popup creates an iframe
// that's same-origin with the opener page. In the iframe we create a service
// worker and then the iframe communicates whether it has observed UseCounter
// kThirdPartyServiceWorker. When storage partitioning is disabled, the
// service worker will be shared between the top-level context and the iframe
// (we could have created the service worker here, for instance). When storage
// partitioning is enabled, though, service workers won't be shared between the
// top-level site and the iframe. Therefore, we need to create the service
// worker in the iframe so that a corresponding service worker is present and
// will cause the kThirdPartyServiceWorker UseCounter to be hit.
promise_test(async t => {
const popup_url = get_host_info().HTTPS_REMOTE_ORIGIN +
'/serviceworker/resources/use-counter-third-party-parent.html';
const popup = window.open(popup_url);
await popup_page_loaded();
return assert_subframe_has_use_counter(popup,
'frame in the popup should have observed the use counter kThirdPartyServiceWorker');
}, 'Test UseCounter for service worker controlled page in third party context.');
</script>