diff options
Diffstat (limited to 'devtools/server/tracer/tests/browser')
5 files changed, 193 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.toml b/devtools/server/tracer/tests/browser/browser.toml new file mode 100644 index 0000000000..61423b42b9 --- /dev/null +++ b/devtools/server/tracer/tests/browser/browser.toml @@ -0,0 +1,11 @@ +[DEFAULT] +tags = "devtools" +subsuite = "devtools" + +["browser_document_tracer.js"] + +["browser_worker_tracer.js"] +support-files = [ + "Worker.tracer.js", + "WorkerDebugger.tracer.js", +] diff --git a/devtools/server/tracer/tests/browser/browser_document_tracer.js b/devtools/server/tracer/tests/browser/browser_document_tracer.js new file mode 100644 index 0000000000..694842fa8b --- /dev/null +++ b/devtools/server/tracer/tests/browser/browser_document_tracer.js @@ -0,0 +1,68 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const JS_CODE = ` +window.onclick = function foo() { + setTimeout(function bar() { + dump("click and timed out\n"); + }); +}; +`; +const TEST_URL = + "data:text/html,<!DOCTYPE html><html><script>" + JS_CODE + " </script>"; + +add_task(async function testTracingWorker() { + const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); + + await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { + const { + addTracingListener, + removeTracingListener, + startTracing, + stopTracing, + } = ChromeUtils.import("resource://devtools/server/tracer/tracer.jsm"); + + // We have to fake opening DevTools otherwise DebuggerNotificationObserver wouldn't work + // and the tracer wouldn't be able to trace the DOM events. + ChromeUtils.notifyDevToolsOpened(); + + const frames = []; + const listener = { + onTracingFrame(frameInfo) { + frames.push(frameInfo); + }, + }; + info("Register a tracing listener"); + addTracingListener(listener); + + info("Start tracing the iframe"); + startTracing({ global: content, traceDOMEvents: true }); + + info("Dispatch a click event on the iframe"); + EventUtils.synthesizeMouseAtCenter( + content.document.documentElement, + {}, + content + ); + + info("Wait for the traces generated by this click"); + await ContentTaskUtils.waitForCondition(() => frames.length == 2); + + const firstFrame = frames[0]; + is(firstFrame.formatedDisplayName, "λ foo"); + is(firstFrame.currentDOMEvent, "DOM(click)"); + + const lastFrame = frames.at(-1); + is(lastFrame.formatedDisplayName, "λ bar"); + is(lastFrame.currentDOMEvent, "setTimeoutCallback"); + + stopTracing(); + removeTracingListener(listener); + + ChromeUtils.notifyDevToolsClosed(); + }); + + BrowserTestUtils.removeTab(tab); +}); 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); + }, + }); + }); +} |