summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-features-browser-toolbox-source-tree.js
blob: 539b2736978b8d2b8a8df685baaed75c327b1f6f (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

/**
 * This test focuses on the SourceTree component, within the browser toolbox.
 */

"use strict";

requestLongerTimeout(2);

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/devtools/client/framework/browser-toolbox/test/helpers-browser-toolbox.js",
  this
);

// Test that the Web extension name is shown in source tree rather than
// the extensions internal UUID. This checks both the web toolbox and the
// browser toolbox.
add_task(async function testSourceTreeNamesForWebExtensions() {
  await pushPref("devtools.chrome.enabled", true);
  await pushPref("devtools.browsertoolbox.scope", "everything");
  const extension = await installAndStartContentScriptExtension();

  const dbg = await initDebugger("doc-content-script-sources.html");
  await waitForSourcesInSourceTree(dbg, [], {
    noExpand: true,
  });

  is(
    getSourceTreeLabel(dbg, 2),
    "Test content script extension",
    "Test content script extension is labeled properly"
  );

  await dbg.toolbox.closeToolbox();
  await extension.unload();

  // Make sure the toolbox opens with the debugger selected.
  await pushPref("devtools.browsertoolbox.panel", "jsdebugger");

  const ToolboxTask = await initBrowserToolboxTask();
  await ToolboxTask.importFunctions({
    createDebuggerContext,
    waitUntil,
    findSourceNodeWithText,
    findAllElements,
    getSelector,
    findAllElementsWithSelector,
    assertSourceTreeNode,
  });

  await ToolboxTask.spawn(selectors, async _selectors => {
    this.selectors = _selectors;
  });

  await ToolboxTask.spawn(null, async () => {
    // Disable autofixing to `Assert` methods which are not available here.
    /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
    try {
      /* global gToolbox */
      // Wait for the debugger to finish loading.
      await gToolbox.getPanelWhenReady("jsdebugger");
      const dbgx = createDebuggerContext(gToolbox);
      let rootNodeForExtensions = null;
      await waitUntil(() => {
        rootNodeForExtensions = findSourceNodeWithText(dbgx, "extension");
        return !!rootNodeForExtensions;
      });
      // Find the root node for extensions and expand it if needed
      if (
        !!rootNodeForExtensions &&
        !rootNodeForExtensions.querySelector(".arrow.expanded")
      ) {
        rootNodeForExtensions.querySelector(".arrow").click();
      }

      // Assert that extensions are displayed in the source tree
      // with their extension name.
      await assertSourceTreeNode(dbgx, "Picture-In-Picture");
      await assertSourceTreeNode(dbgx, "Form Autofill");

      const threadLabels = [...findAllElements(dbgx, "sourceTreeThreads")].map(
        el => {
          return el.textContent;
        }
      );
      is(
        threadLabels[0],
        "Main Thread",
        "The first thread is always the main thread"
      );
      let lastPID = -1,
        lastThreadLabel = "";
      for (let i = 1; i < threadLabels.length; i++) {
        const label = threadLabels[i];
        if (label.startsWith("(pid ")) {
          ok(
            !lastThreadLabel,
            "We should only have content process threads first after the main thread"
          );
          const pid = parseInt(label.match(/pid (\d+)\)/)[1], 10);
          ok(
            pid >= lastPID,
            `The content process threads are sorted by incremental PID ${pid} > ${lastPID}`
          );
          lastPID = pid;
        } else {
          ok(
            label.localeCompare(lastThreadLabel) >= 0,
            `Worker thread labels are sorted alphabeticaly: ${label} vs ${lastThreadLabel}`
          );
          lastThreadLabel = label;
        }
      }
    } catch (e) {
      console.log("Caught exception in spawn", e);
      throw e;
    }
  });

  await ToolboxTask.destroy();
});