browser_aboutdebugging_workers_remote_runtime.js (4748B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const NETWORK_RUNTIME_HOST = "localhost:6080"; 7 const NETWORK_RUNTIME_APP_NAME = "TestNetworkApp"; 8 const USB_RUNTIME_ID = "test-runtime-id"; 9 const USB_RUNTIME_DEVICE_NAME = "test device name"; 10 const USB_RUNTIME_APP_NAME = "TestUsbApp"; 11 12 const TESTS = [ 13 { 14 category: "Other Workers", 15 propertyName: "otherWorkers", 16 workerName: "other/worker/script.js", 17 }, 18 { 19 category: "Service Workers", 20 propertyName: "serviceWorkers", 21 workerName: "service/worker/script.js", 22 }, 23 { 24 category: "Shared Workers", 25 propertyName: "sharedWorkers", 26 workerName: "shared/worker/script.js", 27 }, 28 ]; 29 30 const EMPTY_WORKERS_RESPONSE = { 31 otherWorkers: [], 32 serviceWorkers: [], 33 sharedWorkers: [], 34 }; 35 36 // Test that workers are displayed and updated for remote runtimes when expected. 37 add_task(async function () { 38 const mocks = new Mocks(); 39 40 const { document, tab, window } = await openAboutDebugging({ 41 enableWorkerUpdates: true, 42 }); 43 await selectThisFirefoxPage(document, window.AboutDebugging.store); 44 45 info("Prepare USB client mock"); 46 const usbClient = mocks.createUSBRuntime(USB_RUNTIME_ID, { 47 deviceName: USB_RUNTIME_DEVICE_NAME, 48 name: USB_RUNTIME_APP_NAME, 49 }); 50 mocks.emitUSBUpdate(); 51 52 info("Test addons in runtime page for USB client"); 53 await connectToRuntime(USB_RUNTIME_DEVICE_NAME, document); 54 await waitForRuntimePage(USB_RUNTIME_APP_NAME, document); 55 for (const testData of TESTS) { 56 await testWorkerOnMockedRemoteClient( 57 testData, 58 usbClient, 59 mocks.thisFirefoxClient, 60 document 61 ); 62 } 63 64 info("Prepare Network client mock"); 65 const networkClient = mocks.createNetworkRuntime(NETWORK_RUNTIME_HOST, { 66 name: NETWORK_RUNTIME_APP_NAME, 67 }); 68 69 info("Test workers in runtime page for Network client"); 70 await connectToRuntime(NETWORK_RUNTIME_HOST, document); 71 await waitForRuntimePage(NETWORK_RUNTIME_APP_NAME, document); 72 73 for (const testData of TESTS) { 74 await testWorkerOnMockedRemoteClient( 75 testData, 76 networkClient, 77 mocks.thisFirefoxClient, 78 document 79 ); 80 } 81 82 await removeTab(tab); 83 }); 84 85 /** 86 * Check that workers are visible in the runtime page for a remote client. 87 */ 88 async function testWorkerOnMockedRemoteClient( 89 testData, 90 remoteClient, 91 firefoxClient, 92 document 93 ) { 94 const { category, propertyName, workerName } = testData; 95 info(`Test workers for category [${category}] in remote runtime`); 96 97 const workersPane = getDebugTargetPane(category, document); 98 info("Check an empty target pane message is displayed"); 99 ok( 100 workersPane.querySelector(".qa-debug-target-list-empty"), 101 "Workers list is empty" 102 ); 103 104 info(`Add a worker of type [${propertyName}] to the remote client`); 105 const workers = Object.assign({}, EMPTY_WORKERS_RESPONSE, { 106 [propertyName]: [ 107 { 108 name: workerName, 109 workerDescriptorFront: { 110 actorID: workerName, 111 }, 112 }, 113 ], 114 }); 115 remoteClient.listWorkers = () => workers; 116 remoteClient._eventEmitter.emit("workersUpdated"); 117 118 info("Wait until the worker appears"); 119 await waitUntil( 120 () => !workersPane.querySelector(".qa-debug-target-list-empty") 121 ); 122 123 const workerTarget = findDebugTargetByText(workerName, document); 124 ok(workerTarget, "Worker target appeared for the remote runtime"); 125 126 // Check that the list of REMOTE workers are NOT updated when the local this-firefox 127 // emits a workersUpdated event. 128 info("Remove the worker from the remote client WITHOUT sending an event"); 129 remoteClient.listWorkers = () => EMPTY_WORKERS_RESPONSE; 130 131 info("Simulate a worker update on the ThisFirefox client"); 132 firefoxClient._eventEmitter.emit("workersUpdated"); 133 134 // To avoid wait for a set period of time we trigger another async update, adding a new 135 // tab. We assume that if the worker update mechanism had started, it would also be done 136 // when the new tab was processed. 137 info("Wait until the tab target for 'http://some.random/url.com' appears"); 138 const testTab = { 139 retrieveFavicon: () => {}, 140 outerWindowID: 0, 141 traits: {}, 142 url: "http://some.random/url.com", 143 }; 144 remoteClient.listTabs = () => [testTab]; 145 remoteClient._eventEmitter.emit("tabListChanged"); 146 await waitUntil(() => 147 findDebugTargetByText("http://some.random/url.com", document) 148 ); 149 150 ok( 151 findDebugTargetByText(workerName, document), 152 "The test worker is still visible" 153 ); 154 155 info( 156 "Emit `workersUpdated` on remoteClient and wait for the target list to update" 157 ); 158 remoteClient._eventEmitter.emit("workersUpdated"); 159 await waitUntil(() => !findDebugTargetByText(workerName, document)); 160 }