summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-pretty-print-paused-anonymous.js
blob: 8d3771cae91853703135601884c00d72b30e9741 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* 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/>. */

// Tests that pretty-printing a file with no URL works while paused.

"use strict";

add_task(async function () {
  const dbg = await initDebugger("doc-minified.html");

  info("Evaluate an expression with scriptCommand.execute");
  const debuggerDone = dbg.commands.scriptCommand.execute(
    `debugger; var foo; document.addEventListener("click", e => { debugger; }, {once: true})`
  );
  await waitForPaused(dbg);
  const evaluatedSourceId = dbg.selectors.getSelectedSourceId();

  // This will throw if things fail to pretty-print and render properly.
  info("Pretty print the source created by the evaluated expression");
  await prettyPrint(dbg);

  const prettyEvaluatedSourceFilename =
    evaluatedSourceId.split("/").at(-1) + ":formatted";
  await waitForSource(dbg, prettyEvaluatedSourceFilename);
  const prettySource = findSource(dbg, prettyEvaluatedSourceFilename);

  info("Check that the script was pretty-printed as expected");
  const { value: prettySourceValue } = findSourceContent(dbg, prettySource);

  is(
    prettySourceValue.trim(),
    `debugger;
var foo;
document.addEventListener('click', e => {
  debugger;
}, {
  once: true
})
`.trim(),
    "script was pretty printed as expected"
  );

  await resume(dbg);
  await debuggerDone;

  info("Check if we can pause inside the pretty-printed source");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.document.body.click();
  });
  await waitForPaused(dbg);
  await assertPausedAtSourceAndLine(dbg, prettySource.id, 4);
  await resume(dbg);

  info("Check that pretty printing works in `eval`'d source");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.eval(
      `setTimeout(() => {debugger;document.addEventListener("click", e => { debugger; }, {once: true})}, 100)`
    );
  });
  await waitForPaused(dbg);
  const evalSourceId = dbg.selectors.getSelectedSourceId();

  // This will throw if things fail to pretty-print and render properly.
  info("Pretty print the source created by the `eval` expression");
  await prettyPrint(dbg);

  const prettyEvalSourceFilename =
    evalSourceId.split("/").at(-1) + ":formatted";
  await waitForSource(dbg, prettyEvalSourceFilename);
  const prettyEvalSource = findSource(dbg, prettyEvalSourceFilename);

  info("Check that the script was pretty-printed as expected");
  const { value: prettyEvalSourceValue } = findSourceContent(
    dbg,
    prettyEvalSource
  );

  is(
    prettyEvalSourceValue.trim(),
    `
setTimeout(
  () => {
    debugger;
    document.addEventListener('click', e => {
      debugger;
    }, {
      once: true
    })
  },
  100
)`.trim(),
    "script was pretty printed as expected"
  );
  await resume(dbg);

  info("Check if we can pause inside the pretty-printed eval source");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.document.body.click();
  });
  await waitForPaused(dbg);
  await assertPausedAtSourceAndLine(dbg, prettyEvalSource.id, 5);
  await resume(dbg);

  info("Check that pretty printing works in `new Function` source");
  invokeInTab("breakInNewFunction");
  await waitForPaused(dbg);
  const newFunctionSourceId = dbg.selectors.getSelectedSourceId();

  // This will throw if things fail to pretty-print and render properly.
  info("Pretty print the source created with `new Function`");
  await prettyPrint(dbg);

  const prettyNewFunctionSourceFilename =
    newFunctionSourceId.split("/").at(-1) + ":formatted";
  await waitForSource(dbg, prettyNewFunctionSourceFilename);
  const prettyNewFunctionSource = findSource(
    dbg,
    prettyNewFunctionSourceFilename
  );

  info("Check that the script was pretty-printed as expected");
  const { value: prettyNewFunctionSourceValue } = findSourceContent(
    dbg,
    prettyNewFunctionSource
  );

  is(
    prettyNewFunctionSourceValue.trim(),
    `function anonymous() {
  debugger;
  document.addEventListener('click', function () {
    debugger;
  })
}
`.trim(),
    "script was pretty printed as expected"
  );
  await resume(dbg);

  info("Check if we can pause inside the pretty-printed eval source");
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.document.body.click();
  });
  await waitForPaused(dbg);
  await assertPausedAtSourceAndLine(dbg, prettyNewFunctionSource.id, 4);
  await resume(dbg);
});