From ee49d74c3b71a79a3ef9156dd73fe777ba63b987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Tue, 14 Jul 2020 07:22:48 +0000 Subject: [PATCH] Bug 1643253 - Part 2: Add telemetry mochitests r=jdescottes,Honza Differential Revision: https://phabricator.services.mozilla.com/D82047 --- .../application/test/browser/browser.ini | 4 + ...application_panel_telemetry-select-page.js | 26 ++++ ...ser_application_panel_telemetry-workers.js | 117 ++++++++++++++++++ .../client/application/test/browser/head.js | 36 ++++++ 4 files changed, 183 insertions(+) create mode 100644 devtools/client/application/test/browser/browser_application_panel_telemetry-select-page.js create mode 100644 devtools/client/application/test/browser/browser_application_panel_telemetry-workers.js diff --git a/devtools/client/application/test/browser/browser.ini b/devtools/client/application/test/browser/browser.ini index 8474f3c79653..44313f65d9e0 100644 --- a/devtools/client/application/test/browser/browser.ini +++ b/devtools/client/application/test/browser/browser.ini @@ -55,6 +55,10 @@ skip-if = asan || debug || !serviceworker_e10s # Bug 1559487, 1559591, 1608640 [browser_application_panel_manifest-load.js] [browser_application_panel_manifest-open-json.js] [browser_application_panel_manifest-reload.js] +# Telemetry tests +[browser_application_panel_telemetry-select-page.js] +[browser_application_panel_telemetry-workers.js] +skip-if = asan || debug || !serviceworker_e10s # Bug 1559487, 1559591, 1608640 # Misc tests [browser_application_panel_open-links.js] skip-if = true # Bug 1467256, 1559591 diff --git a/devtools/client/application/test/browser/browser_application_panel_telemetry-select-page.js b/devtools/client/application/test/browser/browser_application_panel_telemetry-select-page.js new file mode 100644 index 000000000000..b4b78e197068 --- /dev/null +++ b/devtools/client/application/test/browser/browser_application_panel_telemetry-select-page.js @@ -0,0 +1,26 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function() { + await enableApplicationPanel(); + + const TAB_URL = URL_ROOT + "resources/service-workers/empty.html"; + const { panel, tab, target } = await openNewTabAndApplicationPanel(TAB_URL); + const doc = panel.panelWin.document; + + setupTelemetryTest(); + + // make sure the default page is opened and then select a different one + await waitUntil(() => doc.querySelector(".js-service-workers-page") !== null); + ok(true, "Service Workers page was loaded per default."); + selectPage(panel, "manifest"); + + checkTelemetryEvent({ method: "select_page", page_type: "manifest" }); + + // close the tab + info("Closing the tab."); + await target.client.waitForRequestsToSettle(); + await BrowserTestUtils.removeTab(tab); +}); diff --git a/devtools/client/application/test/browser/browser_application_panel_telemetry-workers.js b/devtools/client/application/test/browser/browser_application_panel_telemetry-workers.js new file mode 100644 index 000000000000..73331ff2c0c0 --- /dev/null +++ b/devtools/client/application/test/browser/browser_application_panel_telemetry-workers.js @@ -0,0 +1,117 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const TAB_URL = URL_ROOT + "resources/service-workers/simple.html"; + +// check telemetry for unregistering a service worker +add_task(async function() { + await enableApplicationPanel(); + + const { panel, tab, target } = await openNewTabAndApplicationPanel(TAB_URL); + const doc = panel.panelWin.document; + + selectPage(panel, "service-workers"); + + setupTelemetryTest(); + + info("Wait until the service worker appears in the application panel"); + await waitUntil(() => getWorkerContainers(doc).length === 1); + + const workerContainer = getWorkerContainers(doc)[0]; + + info("Wait until the unregister button is displayed for the service worker"); + await waitUntil(() => workerContainer.querySelector(".js-unregister-button")); + info("Click the unregister button"); + const button = workerContainer.querySelector(".js-unregister-button"); + button.click(); + + checkTelemetryEvent({ method: "unregister_worker" }); + + // close the tab + info("Closing the tab."); + await target.client.waitForRequestsToSettle(); + await BrowserTestUtils.removeTab(tab); +}); + +// check telemetry for starting a service worker +add_task(async function() { + info("Set a low service worker idle timeout"); + await pushPref("dom.serviceWorkers.idle_timeout", 1000); + await pushPref("dom.serviceWorkers.idle_extended_timeout", 1000); + + await enableApplicationPanel(); + + const { panel, tab, target } = await openNewTabAndApplicationPanel(TAB_URL); + const doc = panel.panelWin.document; + + selectPage(panel, "service-workers"); + await waitForWorkerRegistration(tab); + + setupTelemetryTest(); + + info("Wait until the service worker appears in the application panel"); + await waitUntil(() => getWorkerContainers(doc).length === 1); + + info("Wait until the start button is displayed and enabled"); + const container = getWorkerContainers(doc)[0]; + await waitUntil(() => { + const button = container.querySelector(".js-start-button"); + return button && !button.disabled; + }); + + info("Click the start button"); + const button = container.querySelector(".js-start-button"); + button.click(); + + checkTelemetryEvent({ method: "start_worker" }); + + // close the tab + info("Closing the tab."); + await target.client.waitForRequestsToSettle(); + await BrowserTestUtils.removeTab(tab); +}); + +// check telemetry for debugging a service worker +add_task(async function() { + await enableApplicationPanel(); + + const { panel, tab, toolbox, target } = await openNewTabAndApplicationPanel( + TAB_URL + ); + + const doc = panel.panelWin.document; + + selectPage(panel, "service-workers"); + setupTelemetryTest(); + + info("Wait until the service worker appears in the application panel"); + await waitUntil(() => getWorkerContainers(doc).length === 1); + + const container = getWorkerContainers(doc)[0]; + info("Wait until the debug button is displayed and enabled"); + await waitUntil(() => { + const button = container.querySelector(".js-debug-button"); + return button && !button.disabled; + }); + + info("Click on the debug button and wait for debugger to be ready"); + const debugButton = container.querySelector(".js-debug-button"); + debugButton.click(); + await waitUntil(() => toolbox.getPanel("jsdebugger")); + + const events = getTelemetryEvents("jsdebugger"); + const openToolboxEvent = events.find(event => event.method == "enter"); + ok(openToolboxEvent.session_id > 0, "Event has a valid session id"); + is( + openToolboxEvent.start_state, + "application", + "Event has the 'application' start state" + ); + + // close the tab + info("Closing the tab."); + await target.client.waitForRequestsToSettle(); + await BrowserTestUtils.removeTab(tab); +}); diff --git a/devtools/client/application/test/browser/head.js b/devtools/client/application/test/browser/head.js index c403166e51c5..1331866e6c84 100644 --- a/devtools/client/application/test/browser/head.js +++ b/devtools/client/application/test/browser/head.js @@ -53,6 +53,42 @@ async function enableApplicationPanel() { await pushPref("devtools.application.enabled", true); } +function setupTelemetryTest() { + // Reset all the counts + Services.telemetry.clearEvents(); + + // Ensure no events have been logged + const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS; + const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true); + ok(!snapshot.parent, "No events have been logged for the main process"); +} + +function getTelemetryEvents(objectName) { + // read the requested events only + const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS; + const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true); + // filter and transform the event data so the relevant info is in a single object: + // { method: "...", extraField: "...", anotherExtraField: "...", ... } + const events = snapshot.parent + .filter(event => event[1] === "devtools.main" && event[3] === objectName) + .map(event => ({ method: event[2], ...event[5] })); + + return events; +} + +function checkTelemetryEvent(expectedEvent, objectName = "application") { + info("Check telemetry event"); + const events = getTelemetryEvents(objectName); + + // assert we only got 1 event with a valid session ID + is(events.length, 1, "There was only 1 event logged"); + const [event] = events; + ok(event.session_id > 0, "There is a valid session_id in the event"); + + // assert expected data + Assert.deepEqual(event, { ...expectedEvent, session_id: event.session_id }); +} + function getWorkerContainers(doc) { return doc.querySelectorAll(".js-sw-container"); }