browser_target_command_service_workers.js (2455B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test the TargetCommand API for service workers in content tabs. 7 8 const FISSION_TEST_URL = URL_ROOT_SSL + "fission_document.html"; 9 10 add_task(async function () { 11 // Disable the preloaded process as it creates processes intermittently 12 // which forces the emission of RDP requests we aren't correctly waiting for. 13 await pushPref("dom.ipc.processPrelaunch.enabled", false); 14 15 info("Setup the test page with workers of all types"); 16 17 const tab = await addTab(FISSION_TEST_URL); 18 19 info("Create a target list for a tab target"); 20 const commands = await CommandsFactory.forTab(tab); 21 const targetCommand = commands.targetCommand; 22 const { TYPES } = targetCommand; 23 24 // Enable Service Worker listening. 25 targetCommand.listenForServiceWorkers = true; 26 await targetCommand.startListening(); 27 28 const serviceWorkerTargets = targetCommand.getAllTargets([ 29 TYPES.SERVICE_WORKER, 30 ]); 31 is( 32 serviceWorkerTargets.length, 33 1, 34 "TargetCommmand has 1 service worker target" 35 ); 36 37 info("Check that the onAvailable is done when watchTargets resolves"); 38 const targets = []; 39 const onAvailable = async ({ targetFront }) => { 40 // Wait for one second here to check that watch targets waits for 41 // the onAvailable callbacks correctly. 42 await wait(1000); 43 targets.push(targetFront); 44 }; 45 const onDestroyed = ({ targetFront }) => 46 targets.splice(targets.indexOf(targetFront), 1); 47 48 await targetCommand.watchTargets({ 49 types: [TYPES.SERVICE_WORKER], 50 onAvailable, 51 onDestroyed, 52 }); 53 54 // We expect onAvailable to have been called one time, for the only service 55 // worker target available in the test page. 56 is(targets.length, 1, "onAvailable has resolved"); 57 is( 58 targets[0], 59 serviceWorkerTargets[0], 60 "onAvailable was called with the expected service worker target" 61 ); 62 63 info("Unregister the worker and wait until onDestroyed is called."); 64 await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { 65 // registrationPromise is set by the test page. 66 const registration = await content.wrappedJSObject.registrationPromise; 67 registration.unregister(); 68 }); 69 await waitUntil(() => targets.length === 0); 70 71 // Stop listening to avoid worker related requests 72 targetCommand.destroy(); 73 74 await commands.waitForRequestsToSettle(); 75 76 await commands.destroy(); 77 });