diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/server/tracer/tests | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/server/tracer/tests')
6 files changed, 267 insertions, 0 deletions
diff --git a/devtools/server/tracer/tests/browser/Worker.tracer.js b/devtools/server/tracer/tests/browser/Worker.tracer.js new file mode 100644 index 0000000000..60db4545a6 --- /dev/null +++ b/devtools/server/tracer/tests/browser/Worker.tracer.js @@ -0,0 +1,10 @@ +"use strict"; + +/* eslint-disable no-unused-vars */ + +function bar() {} +function foo() { + bar(); +} + +postMessage("evaled"); diff --git a/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js b/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js new file mode 100644 index 0000000000..bd6e646b3b --- /dev/null +++ b/devtools/server/tracer/tests/browser/WorkerDebugger.tracer.js @@ -0,0 +1,36 @@ +"use strict"; + +/* global global, loadSubScript */ + +try { + // For some reason WorkerDebuggerGlobalScope.global doesn't expose JS variables + // and we can't call via global.foo(). Instead we have to go throught the Debugger API. + const dbg = new Debugger(global); + const [debuggee] = dbg.getDebuggees(); + + /* global startTracing, stopTracing, addTracingListener, removeTracingListener */ + loadSubScript("resource://devtools/server/tracer/tracer.jsm"); + const frames = []; + const listener = { + onTracingFrame(args) { + frames.push(args); + + // Return true, to also log the trace to stdout + return true; + }, + }; + addTracingListener(listener); + startTracing({ global, prefix: "testWorkerPrefix" }); + + debuggee.executeInGlobal("foo()"); + + stopTracing(); + removeTracingListener(listener); + + // Send the frames to the main thread to do the assertions there. + postMessage(JSON.stringify(frames)); +} catch (e) { + dump( + "Exception while running debugger test script: " + e + "\n" + e.stack + "\n" + ); +} diff --git a/devtools/server/tracer/tests/browser/browser.ini b/devtools/server/tracer/tests/browser/browser.ini new file mode 100644 index 0000000000..fafc59dbef --- /dev/null +++ b/devtools/server/tracer/tests/browser/browser.ini @@ -0,0 +1,8 @@ +[DEFAULT] +tags = devtools +subsuite = devtools + +[browser_worker_tracer.js] +support-files = + Worker.tracer.js + WorkerDebugger.tracer.js diff --git a/devtools/server/tracer/tests/browser/browser_worker_tracer.js b/devtools/server/tracer/tests/browser/browser_worker_tracer.js new file mode 100644 index 0000000000..815da85853 --- /dev/null +++ b/devtools/server/tracer/tests/browser/browser_worker_tracer.js @@ -0,0 +1,68 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const wdm = Cc["@mozilla.org/dom/workers/workerdebuggermanager;1"].getService( + Ci.nsIWorkerDebuggerManager +); + +const BASE_URL = + "chrome://mochitests/content/browser/devtools/server/tracer/tests/browser/"; +const WORKER_URL = BASE_URL + "Worker.tracer.js"; +const WORKER_DEBUGGER_URL = BASE_URL + "WorkerDebugger.tracer.js"; + +add_task(async function testTracingWorker() { + const onDbg = waitForWorkerDebugger(WORKER_URL); + + info("Instantiate a regular worker"); + const worker = new Worker(WORKER_URL); + info("Wait for worker to reply back"); + await new Promise(r => (worker.onmessage = r)); + info("Wait for WorkerDebugger to be instantiated"); + const dbg = await onDbg; + + const onDebuggerScriptSentFrames = new Promise(resolve => { + const listener = { + onMessage(msg) { + dbg.removeListener(listener); + resolve(JSON.parse(msg)); + }, + }; + dbg.addListener(listener); + }); + info("Evaluate a Worker Debugger test script"); + dbg.initialize(WORKER_DEBUGGER_URL); + + info("Wait for frames to be notified by the debugger script"); + const frames = await onDebuggerScriptSentFrames; + + is(frames.length, 3); + // There is a third frame which relates to the usage of Debugger.Object.executeInGlobal + // which we ignore as that's a test side effect. + const lastFrame = frames.at(-1); + const beforeLastFrame = frames.at(-2); + is(beforeLastFrame.depth, 1); + is(beforeLastFrame.formatedDisplayName, "λ foo"); + is(beforeLastFrame.prefix, "testWorkerPrefix: "); + ok(beforeLastFrame.frame); + is(lastFrame.depth, 2); + is(lastFrame.formatedDisplayName, "λ bar"); + is(lastFrame.prefix, "testWorkerPrefix: "); + ok(lastFrame.frame); +}); + +function waitForWorkerDebugger(url, dbgUrl) { + return new Promise(function (resolve) { + wdm.addListener({ + onRegister(dbg) { + if (dbg.url !== url) { + return; + } + ok(true, "Debugger with url " + url + " should be registered."); + wdm.removeListener(this); + resolve(dbg); + }, + }); + }); +} diff --git a/devtools/server/tracer/tests/xpcshell/test_tracer.js b/devtools/server/tracer/tests/xpcshell/test_tracer.js new file mode 100644 index 0000000000..4150944d88 --- /dev/null +++ b/devtools/server/tracer/tests/xpcshell/test_tracer.js @@ -0,0 +1,139 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { addTracingListener, removeTracingListener, startTracing, stopTracing } = + ChromeUtils.import("resource://devtools/server/tracer/tracer.jsm"); + +add_task(async function () { + // Because this test uses evalInSandbox, we need to tweak the following prefs + Services.prefs.setBoolPref( + "security.allow_parent_unrestricted_js_loads", + true + ); + registerCleanupFunction(() => { + Services.prefs.clearUserPref("security.allow_parent_unrestricted_js_loads"); + }); +}); + +add_task(async function testTracingContentGlobal() { + const toggles = []; + const frames = []; + const listener = { + onTracingToggled(state) { + toggles.push(state); + }, + onTracingFrame(frameInfo) { + frames.push(frameInfo); + }, + }; + + info("Register a tracing listener"); + addTracingListener(listener); + + const sandbox = Cu.Sandbox("https://example.com"); + Cu.evalInSandbox("function bar() {}; function foo() {bar()};", sandbox); + + info("Start tracing"); + startTracing({ global: sandbox, prefix: "testContentPrefix" }); + Assert.equal(toggles.length, 1); + Assert.equal(toggles[0], true); + + info("Call some code"); + sandbox.foo(); + + Assert.equal(frames.length, 2); + const lastFrame = frames.pop(); + const beforeLastFrame = frames.pop(); + Assert.equal(beforeLastFrame.depth, 0); + Assert.equal(beforeLastFrame.formatedDisplayName, "λ foo"); + Assert.equal(beforeLastFrame.prefix, "testContentPrefix: "); + Assert.ok(beforeLastFrame.frame); + Assert.equal(lastFrame.depth, 1); + Assert.equal(lastFrame.formatedDisplayName, "λ bar"); + Assert.equal(lastFrame.prefix, "testContentPrefix: "); + Assert.ok(lastFrame.frame); + + info("Stop tracing"); + stopTracing(); + Assert.equal(toggles.length, 2); + Assert.equal(toggles[1], false); + + info("Recall code after stop, no more traces are logged"); + sandbox.foo(); + Assert.equal(frames.length, 0); + + info("Start tracing again, and recall code"); + startTracing({ global: sandbox, prefix: "testContentPrefix" }); + sandbox.foo(); + info("New traces are logged"); + Assert.equal(frames.length, 2); + + info("Unregister the listener and recall code"); + removeTracingListener(listener); + sandbox.foo(); + info("No more traces are logged"); + Assert.equal(frames.length, 2); + + info("Stop tracing"); + stopTracing(); +}); + +add_task(async function testTracingJSMGlobal() { + // We have to register the listener code in a sandbox, i.e. in a distinct global + // so that we aren't creating traces when tracer calls it. (and cause infinite loops) + const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); + const listenerSandbox = Cu.Sandbox(systemPrincipal); + Cu.evalInSandbox( + "new " + + function () { + globalThis.toggles = []; + globalThis.frames = []; + globalThis.listener = { + onTracingToggled(state) { + globalThis.toggles.push(state); + }, + onTracingFrame(frameInfo) { + globalThis.frames.push(frameInfo); + }, + }; + }, + listenerSandbox + ); + + info("Register a tracing listener"); + addTracingListener(listenerSandbox.listener); + + info("Start tracing"); + startTracing({ global: null, prefix: "testPrefix" }); + Assert.equal(listenerSandbox.toggles.length, 1); + Assert.equal(listenerSandbox.toggles[0], true); + + info("Call some code"); + function bar() {} + function foo() { + bar(); + } + foo(); + + // Note that the tracer will record the two Assert.equal and the info calls. + // So only assert the last two frames. + const lastFrame = listenerSandbox.frames.at(-1); + const beforeLastFrame = listenerSandbox.frames.at(-2); + Assert.equal(beforeLastFrame.depth, 0); + Assert.equal(beforeLastFrame.formatedDisplayName, "λ foo"); + Assert.equal(beforeLastFrame.prefix, "testPrefix: "); + Assert.ok(beforeLastFrame.frame); + Assert.equal(lastFrame.depth, 1); + Assert.equal(lastFrame.formatedDisplayName, "λ bar"); + Assert.equal(lastFrame.prefix, "testPrefix: "); + Assert.ok(lastFrame.frame); + + info("Stop tracing"); + stopTracing(); + Assert.equal(listenerSandbox.toggles.length, 2); + Assert.equal(listenerSandbox.toggles[1], false); + + removeTracingListener(listenerSandbox.listener); +}); diff --git a/devtools/server/tracer/tests/xpcshell/xpcshell.ini b/devtools/server/tracer/tests/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..4864934b9e --- /dev/null +++ b/devtools/server/tracer/tests/xpcshell/xpcshell.ini @@ -0,0 +1,6 @@ +[DEFAULT] +tags = devtools +firefox-appdir = browser +skip-if = toolkit == 'android' + +[test_tracer.js] |