windowclient-navigate-worker.js (2327B)
1 importScripts('/resources/testharness.js'); 2 3 function matchQuery(queryString) { 4 return self.location.search.substr(1) === queryString; 5 } 6 7 async function navigateTest(t, e) { 8 const port = e.data.port; 9 const url = e.data.url; 10 const expected = e.data.expected; 11 12 let p = clients.matchAll({ includeUncontrolled : true }) 13 .then(function(clients) { 14 for (const client of clients) { 15 if (client.url === e.data.clientUrl) { 16 assert_equals(client.frameType, e.data.frameType); 17 return client.navigate(url); 18 } 19 } 20 throw 'Could not locate window client.'; 21 }).then(function(newClient) { 22 // If we didn't reject, we better get resolved with the right thing. 23 if (newClient === null) { 24 assert_equals(newClient, expected); 25 } else { 26 assert_equals(newClient.url, expected); 27 } 28 }); 29 30 if (typeof self[expected] === "function") { 31 // It's a JS error type name. We are expecting our promise to be rejected 32 // with that error. 33 p = promise_rejects_js(t, self[expected], p); 34 } 35 36 // Let our caller know we are done. 37 return p.finally(() => port.postMessage(null)); 38 } 39 40 function getTestClient() { 41 return clients.matchAll({ includeUncontrolled: true }) 42 .then(function(clients) { 43 for (const client of clients) { 44 if (client.url.includes('windowclient-navigate.https.html')) { 45 return client; 46 } 47 } 48 49 throw new Error('Service worker was unable to locate test client.'); 50 }); 51 } 52 53 function waitForMessage(client) { 54 const channel = new MessageChannel(); 55 client.postMessage({ port: channel.port2 }, [channel.port2]); 56 57 return new Promise(function(resolve) { 58 channel.port1.onmessage = resolve; 59 }); 60 } 61 62 // The worker must remain in the "installing" state for the duration of some 63 // sub-tests. In order to achieve this coordination without relying on global 64 // state, the worker must create a message channel with the client from within 65 // the "install" event handler. 66 if (matchQuery('installing')) { 67 self.addEventListener('install', function(e) { 68 e.waitUntil(getTestClient().then(waitForMessage)); 69 }); 70 } 71 72 self.addEventListener('message', function(e) { 73 e.waitUntil(promise_test(t => navigateTest(t, e), 74 e.data.description + " worker side")); 75 });