diff --git a/toolkit/components/extensions/test/xpcshell/head_telemetry.js b/toolkit/components/extensions/test/xpcshell/head_telemetry.js new file mode 100644 index 000000000000..98b87d14d54d --- /dev/null +++ b/toolkit/components/extensions/test/xpcshell/head_telemetry.js @@ -0,0 +1,34 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +/* exported IS_OOP, arraySum, clearHistograms, getSnapshots, promiseTelemetryRecorded */ + +XPCOMUtils.defineLazyModuleGetter(this, "ContentTaskUtils", + "resource://testing-common/ContentTaskUtils.jsm"); + +const IS_OOP = Services.prefs.getBoolPref("extensions.webextensions.remote"); + +function arraySum(arr) { + return arr.reduce((a, b) => a + b, 0); +} + +function clearHistograms() { + Services.telemetry.snapshotSubsessionHistograms(true); +} + +function getSnapshots(process) { + return Services.telemetry.snapshotSubsessionHistograms()[process]; +} + +// There is no good way to make sure that the parent received the histogram +// entries from the extension and content processes. +// Let's stick to the ugly, spinning the event loop until we have a good +// approach (Bug 1357509). +function promiseTelemetryRecorded(id, process, expectedCount) { + let condition = () => { + let snapshot = Services.telemetry.snapshotSubsessionHistograms()[process][id]; + return snapshot && arraySum(snapshot.counts) >= expectedCount; + }; + return ContentTaskUtils.waitForCondition(condition); +} diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_extension_content_telemetry.js b/toolkit/components/extensions/test/xpcshell/test_ext_extension_content_telemetry.js index 91dae96deabf..fc71a40a6f10 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_extension_content_telemetry.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_extension_content_telemetry.js @@ -41,32 +41,35 @@ add_task(async function test_telemetry() { }, }); - let histogram = Services.telemetry.getHistogramById(HISTOGRAM); - histogram.clear(); - equal(histogram.snapshot().sum, 0, - `No data recorded for histogram: ${HISTOGRAM}.`); + clearHistograms(); + + let process = IS_OOP ? "content" : "parent"; + ok(!(HISTOGRAM in getSnapshots(process)), `No data recorded for histogram: ${HISTOGRAM}.`); await extension1.startup(); - equal(histogram.snapshot().sum, 0, - `No data recorded for histogram after startup: ${HISTOGRAM}.`); + ok(!(HISTOGRAM in getSnapshots(process)), + `No data recorded for histogram after startup: ${HISTOGRAM}.`); let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`); await extension1.awaitMessage("content-script-run"); - let histogramSum = histogram.snapshot().sum; - ok(histogramSum > 0, - `Data recorded for first extension for histogram: ${HISTOGRAM}.`); + await promiseTelemetryRecorded(HISTOGRAM, process, 1); + + equal(arraySum(getSnapshots(process)[HISTOGRAM].counts), 1, + `Data recorded for histogram: ${HISTOGRAM}.`); await contentPage.close(); await extension1.unload(); await extension2.startup(); - equal(histogram.snapshot().sum, histogramSum, + equal(arraySum(getSnapshots(process)[HISTOGRAM].counts), 1, `No data recorded for histogram after startup: ${HISTOGRAM}.`); contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`); await extension2.awaitMessage("content-script-run"); - ok(histogram.snapshot().sum > histogramSum, - `Data recorded for second extension for histogram: ${HISTOGRAM}.`); + await promiseTelemetryRecorded(HISTOGRAM, process, 2); + + equal(arraySum(getSnapshots(process)[HISTOGRAM].counts), 2, + `Data recorded for histogram: ${HISTOGRAM}.`); await contentPage.close(); await extension2.unload(); diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_storage_telemetry.js b/toolkit/components/extensions/test/xpcshell/test_ext_storage_telemetry.js index 3d335cf756dc..cce0499c4378 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_storage_telemetry.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_storage_telemetry.js @@ -6,10 +6,6 @@ const HISTOGRAM_IDS = [ "WEBEXT_STORAGE_LOCAL_SET_MS", "WEBEXT_STORAGE_LOCAL_GET_MS", ]; -function arraySum(arr) { - return arr.reduce((a, b) => a + b, 0); -} - add_task(async function test_telemetry_background() { const server = createHttpServer(); server.registerDirectory("/data/", do_get_file("data")); @@ -49,44 +45,58 @@ add_task(async function test_telemetry_background() { let extension1 = ExtensionTestUtils.loadExtension(extInfo); let extension2 = ExtensionTestUtils.loadExtension(extInfo); - // Initialize and clear histograms. - let histograms = {}; + clearHistograms(); + + let process = IS_OOP ? "extension" : "parent"; + let snapshots = getSnapshots(process); for (let id of HISTOGRAM_IDS) { - histograms[id] = Services.telemetry.getHistogramById(id); - histograms[id].clear(); - equal(arraySum(histograms[id].snapshot().counts), 0, - `No data recorded for histogram: ${id}.`); + ok(!(id in snapshots), `No data recorded for histogram: ${id}.`); } await extension1.startup(); await extension1.awaitMessage("backgroundDone"); + for (let id of HISTOGRAM_IDS) { + await promiseTelemetryRecorded(id, process, 1); + } // Telemetry from extension1's background page should be recorded. - for (let id in histograms) { - equal(arraySum(histograms[id].snapshot().counts), 1, + snapshots = getSnapshots(process); + for (let id of HISTOGRAM_IDS) { + equal(arraySum(snapshots[id].counts), 1, `Data recorded for histogram: ${id}.`); } await extension2.startup(); await extension2.awaitMessage("backgroundDone"); + for (let id of HISTOGRAM_IDS) { + await promiseTelemetryRecorded(id, process, 2); + } // Telemetry from extension2's background page should be recorded. - for (let id in histograms) { - equal(arraySum(histograms[id].snapshot().counts), 2, + snapshots = getSnapshots(process); + for (let id of HISTOGRAM_IDS) { + equal(arraySum(snapshots[id].counts), 2, `Additional data recorded for histogram: ${id}.`); } await extension2.unload(); // Run a content script. + process = IS_OOP ? "content" : "parent"; + let expectedCount = IS_OOP ? 1 : 3; let contentScriptPromise = extension1.awaitMessage("contentDone"); let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`); await contentScriptPromise; await contentPage.close(); + for (let id of HISTOGRAM_IDS) { + await promiseTelemetryRecorded(id, process, expectedCount); + } + // Telemetry from extension1's content script should be recorded. - for (let id in histograms) { - equal(arraySum(histograms[id].snapshot().counts), 3, + snapshots = getSnapshots(process); + for (let id of HISTOGRAM_IDS) { + equal(arraySum(snapshots[id].counts), expectedCount, `Data recorded in content script for histogram: ${id}.`); } diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini index c8f290f67347..912e93a7713a 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini @@ -13,6 +13,7 @@ skip-if = os == "android" # Android does not use Places for history. [test_ext_background_telemetry.js] [test_ext_background_window_properties.js] skip-if = os == "android" +[test_ext_browserSettings.js] [test_ext_contextual_identities.js] skip-if = os == "android" # Containers are not exposed to android. [test_ext_debugging_utils.js] @@ -27,6 +28,8 @@ skip-if = os == "android" [test_ext_extension.js] [test_ext_extensionPreferencesManager.js] [test_ext_extensionSettingsStore.js] +[test_ext_extension_content_telemetry.js] +skip-if = os == "android" # checking for telemetry needs to be updated: 1384923 [test_ext_extension_startup_telemetry.js] [test_ext_idle.js] [test_ext_legacy_extension_context.js] @@ -56,6 +59,8 @@ head = head.js head_sync.js skip-if = os == "android" [test_ext_storage_sync_crypto.js] skip-if = os == "android" +[test_ext_storage_telemetry.js] +skip-if = os == "android" # checking for telemetry needs to be updated: 1384923 [test_ext_topSites.js] skip-if = os == "android" [test_native_messaging.js] diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini b/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini index 8af28a476edc..31cec2a7e1c0 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini @@ -1,5 +1,5 @@ [DEFAULT] -head = head.js head_remote.js head_e10s.js +head = head.js head_remote.js head_e10s.js head_telemetry.js tail = firefox-appdir = browser skip-if = appname == "thunderbird" || os == "android" diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell.ini b/toolkit/components/extensions/test/xpcshell/xpcshell.ini index b9ee0c04a497..c5d4680c0786 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini @@ -1,5 +1,5 @@ [DEFAULT] -head = head.js +head = head.js head_telemetry.js firefox-appdir = browser skip-if = appname == "thunderbird" dupe-manifest =