browser_getProcess.js (4009B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * Test `RootActor.getProcess` method 6 */ 7 8 "use strict"; 9 10 add_task(async () => { 11 let client, tab; 12 13 function connect() { 14 // Fake a first connection to the content process 15 const transport = DevToolsServer.connectPipe(); 16 client = new DevToolsClient(transport); 17 return client.connect(); 18 } 19 20 async function listProcess() { 21 const onNewProcess = new Promise(resolve => { 22 // Call listProcesses in order to start receiving new process notifications 23 client.mainRoot.on("processListChanged", function listener() { 24 client.off("processListChanged", listener); 25 ok(true, "Received processListChanged event"); 26 resolve(); 27 }); 28 }); 29 await client.mainRoot.listProcesses(); 30 await createNewProcess(); 31 return onNewProcess; 32 } 33 34 async function createNewProcess() { 35 tab = await BrowserTestUtils.openNewForegroundTab({ 36 gBrowser, 37 url: "data:text/html,new-process", 38 forceNewProcess: true, 39 }); 40 } 41 42 async function getProcess() { 43 // Note that we can't assert process count as the number of processes 44 // is affected by previous tests. 45 const processes = await client.mainRoot.listProcesses(); 46 const { osPid } = tab.linkedBrowser.browsingContext.currentWindowGlobal; 47 const descriptor = processes.find(process => process.id == osPid); 48 ok(descriptor, "Got the new process descriptor"); 49 50 // Connect to the first content process available 51 const content = processes.filter(p => !p.isParentProcessDescriptor)[0]; 52 53 const processDescriptor = await client.mainRoot.getProcess(content.id); 54 const front = await processDescriptor.getTarget(); 55 const targetForm = front.targetForm; 56 ok(targetForm.consoleActor, "Got the console actor"); 57 ok(targetForm.threadActor, "Got the thread actor"); 58 59 // Process target are no longer really used/supported beyond listing their workers 60 // from RootFront. 61 const { workers } = await front.listWorkers(); 62 is(workers.length, 0, "listWorkers worked and reported no workers"); 63 64 return [front, content.id]; 65 } 66 67 // Assert that calling client.getProcess against the same process id is 68 // returning the same actor. 69 async function getProcessAgain(firstTargetFront, id) { 70 const processDescriptor = await client.mainRoot.getProcess(id); 71 const front = await processDescriptor.getTarget(); 72 is( 73 front, 74 firstTargetFront, 75 "Second call to getProcess with the same id returns the same form" 76 ); 77 } 78 79 function processScript() { 80 /* eslint-env mozilla/process-script */ 81 const listener = function () { 82 Services.obs.removeObserver(listener, "devtools:loader:destroy"); 83 sendAsyncMessage("test:getProcess-destroy", null); 84 }; 85 Services.obs.addObserver(listener, "devtools:loader:destroy"); 86 } 87 88 async function closeClient() { 89 const onLoaderDestroyed = new Promise(done => { 90 const processListener = function () { 91 Services.ppmm.removeMessageListener( 92 "test:getProcess-destroy", 93 processListener 94 ); 95 done(); 96 }; 97 Services.ppmm.addMessageListener( 98 "test:getProcess-destroy", 99 processListener 100 ); 101 }); 102 const script = `data:,(${encodeURI(processScript)})()`; 103 Services.ppmm.loadProcessScript(script, true); 104 await client.close(); 105 106 await onLoaderDestroyed; 107 Services.ppmm.removeDelayedProcessScript(script); 108 info("Loader destroyed in the content process"); 109 } 110 111 // Instantiate a minimal server 112 DevToolsServer.init(); 113 DevToolsServer.allowChromeProcess = true; 114 if (!DevToolsServer.createRootActor) { 115 DevToolsServer.registerAllActors(); 116 } 117 118 await connect(); 119 await listProcess(); 120 121 const [front, contentId] = await getProcess(); 122 123 await getProcessAgain(front, contentId); 124 125 await closeClient(); 126 127 BrowserTestUtils.removeTab(tab); 128 DevToolsServer.destroy(); 129 });