browser_target_command_getAllTargets.js (3822B)
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 getAllTargets. 7 8 const FISSION_TEST_URL = URL_ROOT_SSL + "fission_document.html"; 9 const CHROME_WORKER_URL = CHROME_URL_ROOT + "test_worker.js"; 10 11 add_task(async function () { 12 // Disable the preloaded process as it creates processes intermittently 13 // which forces the emission of RDP requests we aren't correctly waiting for. 14 await pushPref("dom.ipc.processPrelaunch.enabled", false); 15 16 info("Setup the test page with workers of all types"); 17 18 const tab = await addTab(FISSION_TEST_URL); 19 20 // Instantiate a worker in the parent process 21 // eslint-disable-next-line no-unused-vars 22 const worker = new Worker(CHROME_WORKER_URL + "#simple-worker"); 23 // eslint-disable-next-line no-unused-vars 24 const sharedWorker = new SharedWorker(CHROME_WORKER_URL + "#shared-worker"); 25 26 info("Create a target list for the main process target"); 27 const commands = await CommandsFactory.forMainProcess(); 28 const targetCommand = commands.targetCommand; 29 const { TYPES } = targetCommand; 30 await targetCommand.startListening(); 31 32 info("Check getAllTargets will throw when providing invalid arguments"); 33 Assert.throws( 34 () => targetCommand.getAllTargets(), 35 e => e.message === "getAllTargets expects a non-empty array of types" 36 ); 37 38 Assert.throws( 39 () => targetCommand.getAllTargets([]), 40 e => e.message === "getAllTargets expects a non-empty array of types" 41 ); 42 43 info("Check getAllTargets returns consistent results with several types"); 44 const workerTargets = targetCommand.getAllTargets([TYPES.WORKER]); 45 const serviceWorkerTargets = targetCommand.getAllTargets([ 46 TYPES.SERVICE_WORKER, 47 ]); 48 const sharedWorkerTargets = targetCommand.getAllTargets([ 49 TYPES.SHARED_WORKER, 50 ]); 51 const processTargets = targetCommand.getAllTargets([TYPES.PROCESS]); 52 const frameTargets = targetCommand.getAllTargets([TYPES.FRAME]); 53 54 const allWorkerTargetsReference = [ 55 ...workerTargets, 56 ...serviceWorkerTargets, 57 ...sharedWorkerTargets, 58 ]; 59 const allWorkerTargets = targetCommand.getAllTargets([ 60 TYPES.WORKER, 61 TYPES.SERVICE_WORKER, 62 TYPES.SHARED_WORKER, 63 ]); 64 65 is( 66 allWorkerTargets.length, 67 allWorkerTargetsReference.length, 68 "getAllTargets([worker, service, shared]) returned the expected number of targets" 69 ); 70 71 ok( 72 allWorkerTargets.every(t => allWorkerTargetsReference.includes(t)), 73 "getAllTargets([worker, service, shared]) returned the expected targets" 74 ); 75 76 const allTargetsReference = [ 77 ...allWorkerTargets, 78 ...processTargets, 79 ...frameTargets, 80 ]; 81 const allTargets = targetCommand.getAllTargets(targetCommand.ALL_TYPES); 82 is( 83 allTargets.length, 84 allTargetsReference.length, 85 "getAllTargets(ALL_TYPES) returned the expected number of targets" 86 ); 87 88 ok( 89 allTargets.every(t => allTargetsReference.includes(t)), 90 "getAllTargets(ALL_TYPES) returned the expected targets" 91 ); 92 93 for (const target of allTargets) { 94 is( 95 target.commands, 96 commands, 97 "Each target front has a `commands` attribute - " + target 98 ); 99 } 100 101 // Wait for all the targets to be fully attached so we don't have pending requests. 102 await waitForAllTargetsToBeAttached(targetCommand); 103 104 ok( 105 !targetCommand.isDestroyed(), 106 "TargetCommand isn't destroyed before calling commands.destroy()" 107 ); 108 await commands.destroy(); 109 ok( 110 targetCommand.isDestroyed(), 111 "TargetCommand is destroyed after calling commands.destroy()" 112 ); 113 114 await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { 115 // registrationPromise is set by the test page. 116 const registration = await content.wrappedJSObject.registrationPromise; 117 registration.unregister(); 118 }); 119 });