summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html')
-rw-r--r--toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html112
1 files changed, 112 insertions, 0 deletions
diff --git a/toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html b/toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html
new file mode 100644
index 0000000000..d2bb66d507
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_contentscript_telemetry.html
@@ -0,0 +1,112 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Test for telemetry for content script injection</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
+ <script src="head.js"></script>
+ <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script>
+"use strict";
+
+const HISTOGRAM = "WEBEXT_CONTENT_SCRIPT_INJECTION_MS";
+const GLEAN_METRIC_ID = "contentScriptInjection";
+
+function assertHistogramSamplesCount(histogram, expectedSamplesCount, msg) {
+ return is(
+ Object.values(histogram.snapshot().values).reduce((a, b) => a + b, 0),
+ expectedSamplesCount,
+ msg
+ );
+}
+
+add_task(async function test_contentscript_telemetry() {
+ // Turn on telemetry and reset it to the previous state once the test is completed.
+ const telemetryCanRecordBase = SpecialPowers.Services.telemetry.canRecordBase;
+ SpecialPowers.Services.telemetry.canRecordBase = true;
+ SimpleTest.registerCleanupFunction(() => {
+ SpecialPowers.Services.telemetry.canRecordBase = telemetryCanRecordBase;
+ });
+
+ function background() {
+ browser.test.onMessage.addListener(() => {
+ browser.tabs.executeScript({code: 'browser.test.sendMessage("content-script-run");'});
+ });
+ }
+
+ let extensionData = {
+ manifest: {
+ permissions: ["<all_urls>"],
+ },
+ background,
+ };
+
+ let tab = await AppTestDelegate.openNewForegroundTab(
+ window,
+ "https://example.com",
+ true
+ );
+
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+
+ await Services.fog.testFlushAllChildren();
+ Services.fog.testResetFOG();
+
+ is(
+ Glean.extensionsTiming[GLEAN_METRIC_ID].testGetValue(),
+ null,
+ `No data recorded for Glean metric extensionsTiming.${GLEAN_METRIC_ID}`
+ );
+
+ let histogram = SpecialPowers.Services.telemetry.getHistogramById(HISTOGRAM);
+ histogram.clear();
+ assertHistogramSamplesCount(
+ histogram,
+ 0,
+ `No data recorded for histogram: ${HISTOGRAM}.`
+ );
+
+ await extension.startup();
+
+ assertHistogramSamplesCount(
+ histogram,
+ 0,
+ `No data recorded for histogram: ${HISTOGRAM}.`
+ );
+
+ await Services.fog.testFlushAllChildren();
+ is(
+ Glean.extensionsTiming[GLEAN_METRIC_ID].testGetValue(),
+ null,
+ `No data recorded for Glean metric extensionsTiming.${GLEAN_METRIC_ID}`
+ );
+
+ extension.sendMessage();
+ await extension.awaitMessage("content-script-run");
+
+ await Services.fog.testFlushAllChildren();
+
+ ok(
+ Glean.extensionsTiming[GLEAN_METRIC_ID].testGetValue()?.sum > 0,
+ `Data recorded for first extension on Glean metric extensionsTiming.${GLEAN_METRIC_ID}`
+ );
+
+ // Asserting the number of samples is the expected one (the histogram sum value
+ // is intermittently set to 0 due to a sample being intermittently recorded for
+ // the bucket 0 and would trigger an intermittent failure, see Bug 1864213).
+ assertHistogramSamplesCount(
+ histogram,
+ 1,
+ `Data recorded for first extension for histogram: ${HISTOGRAM}.`
+ );
+
+ await AppTestDelegate.removeTab(window, tab);
+ await extension.unload();
+});
+</script>
+
+</body>
+</html>