head.js (4484B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* eslint no-unused-vars: [2, {"vars": "local"}] */ 5 6 "use strict"; 7 8 // Load the shared-head file first. 9 Services.scriptloader.loadSubScript( 10 "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js", 11 this 12 ); 13 14 /** 15 * Set all preferences needed to enable service worker debugging and testing. 16 */ 17 async function enableServiceWorkerDebugging() { 18 // Enable service workers. 19 await pushPref("dom.serviceWorkers.enabled", true); 20 // Accept workers from mochitest's http. 21 await pushPref("dom.serviceWorkers.testing.enabled", true); 22 // Force single content process, see Bug 1231208 for the SW refactor that should enable 23 // SW debugging in multi-e10s. 24 await pushPref("dom.ipc.processCount", 1); 25 26 // Disable randomly spawning processes during tests 27 await pushPref("dom.ipc.processPrelaunch.enabled", false); 28 29 // Wait for dom.ipc.processCount to be updated before releasing processes. 30 Services.ppmm.releaseCachedProcesses(); 31 } 32 33 async function enableApplicationPanel() { 34 // FIXME bug 1575427 this rejection is very common. 35 const { PromiseTestUtils } = ChromeUtils.importESModule( 36 "resource://testing-common/PromiseTestUtils.sys.mjs" 37 ); 38 PromiseTestUtils.allowMatchingRejectionsGlobally( 39 /this._frontCreationListeners is null/ 40 ); 41 42 // Enable all preferences related to service worker debugging. 43 await enableServiceWorkerDebugging(); 44 45 // Enable web manifest processing. 46 Services.prefs.setBoolPref("dom.manifest.enabled", true); 47 48 // Enable application panel in DevTools. 49 await pushPref("devtools.application.enabled", true); 50 } 51 52 function setupTelemetryTest() { 53 // Reset all the counts 54 Services.telemetry.clearEvents(); 55 56 // Ensure no events have been logged 57 const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS; 58 const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true); 59 ok(!snapshot.parent, "No events have been logged for the main process"); 60 } 61 62 function getTelemetryEvents(objectName) { 63 // read the requested events only 64 const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS; 65 const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true); 66 // filter and transform the event data so the relevant info is in a single object: 67 // { method: "...", extraField: "...", anotherExtraField: "...", ... } 68 const events = snapshot.parent 69 .filter(event => event[1] === "devtools.main" && event[3] === objectName) 70 .map(event => ({ method: event[2], ...event[5] })); 71 72 return events; 73 } 74 75 function checkTelemetryEvent(expectedEvent, objectName = "application") { 76 info("Check telemetry event"); 77 const events = getTelemetryEvents(objectName); 78 79 // assert we only got 1 event with a valid session ID 80 is(events.length, 1, "There was only 1 event logged"); 81 const [event] = events; 82 Assert.greater( 83 Number(event.session_id), 84 0, 85 "There is a valid session_id in the event" 86 ); 87 88 // assert expected data 89 Assert.deepEqual(event, { ...expectedEvent, session_id: event.session_id }); 90 } 91 92 function getWorkerContainers(doc) { 93 return doc.querySelectorAll(".js-sw-container"); 94 } 95 96 async function openNewTabAndApplicationPanel(url) { 97 const tab = await addTab(url); 98 99 const toolbox = await gDevTools.showToolboxForTab(tab, { 100 toolId: "application", 101 }); 102 const panel = toolbox.getCurrentPanel(); 103 const target = toolbox.target; 104 const commands = toolbox.commands; 105 return { panel, tab, target, toolbox, commands }; 106 } 107 108 async function unregisterAllWorkers(client, doc) { 109 // This method is declared in shared-head.js 110 await unregisterAllServiceWorkers(client); 111 112 info("Wait for service workers to disappear from the UI"); 113 waitUntil(() => getWorkerContainers(doc).length === 0); 114 } 115 116 async function waitForWorkerRegistration(swTab) { 117 info("Wait until the registration appears on the window"); 118 const swBrowser = swTab.linkedBrowser; 119 await asyncWaitUntil(async () => 120 SpecialPowers.spawn(swBrowser, [], function () { 121 return !!content.wrappedJSObject.getRegistration(); 122 }) 123 ); 124 } 125 126 function selectPage(panel, page) { 127 /** 128 * Select a page by simulating a user click in the sidebar. 129 * 130 * @param {string} page The page we want to select (see `PAGE_TYPES`) 131 */ 132 info(`Selecting application page: ${page}`); 133 const doc = panel.panelWin.document; 134 const navItem = doc.querySelector(`.js-sidebar-${page}`); 135 navItem.click(); 136 }