summaryrefslogtreecommitdiffstats
path: root/devtools/server/tracer/tests/browser/browser_document_tracer.js
blob: dcf2c9eb4db645e03790f681d152410ef4953dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
});