summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-pretty-print-sourcemap.js
blob: c7e9e3f06f74872e52c3a06dd368588955cea51c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 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 tests pretty-printing for sourcemapped files.

"use strict";

const httpServer = createTestHTTPServer();
httpServer.registerContentType("html", "text/html");
httpServer.registerContentType("js", "application/javascript");

httpServer.registerPathHandler(
  "/doc-prettyprint-sourcemap.html",
  (request, response) => {
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.write(`<!DOCTYPE html>
    <html>
      <head>
        <script type="text/javascript" src="/js1.min.js"></script>
        <script type="text/javascript" src="/js2.min.js"></script>
      </head>
      <script>
        const a = 3;
        console.log(a);
      </script>
    </html>
  `);
  }
);

function sourceHandler(request, response) {
  response.setHeader("Content-Type", "text/javascript");
  console.log(request.path.substring(1));
  response.write(`function add(a,b,k){var result=a+b;return k(result)}function sub(a,b,k){var result=a-b;return k(result)}function mul(a,b,k){var result=a*b;return k(result)}function div(a,b,k){var result=a/b;return k(result)}function arithmetic(){add(4,4,function(a){sub(a,2,function(b){mul(b,3,function(c){div(c,2,function(d){console.log("arithmetic",d)})})})})};
//# sourceMappingURL=${request.path.substring(1)}.map`);
}

httpServer.registerPathHandler("/js1.min.js", sourceHandler);
httpServer.registerPathHandler("/js2.min.js", sourceHandler);

// This returns no sourcemap
httpServer.registerPathHandler("/js1.min.js.map", (request, response) => {
  response.setHeader("Content-Type", "application/javascript");
  response.setStatusLine(request.httpVersion, 404, "Not found");
  response.write(`console.log("http error")`);
});

// This returns a sourcemap without any original source
httpServer.registerPathHandler("/js2.min.js.map", (request, response) => {
  response.setHeader("Content-Type", "application/javascript");
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.write(
    `{"version":3,"sources":[],"names":["message","document","getElementById","logMessage","console","log","value","addEventListener","logMessageButton"],"mappings":"AAAA,GAAIA,SAAUC,SAASC,eAAe,UAEtC,SAASC,cACPC,QAAQC,IAAI,eAAiBL,QAAQM,OAGvCN,QAAQO,iBAAiB,QAAS,WAChCP,QAAQM,MAAQ,IACf,MAEH,IAAIE,kBAAmBP,SAASC,eAAe,cAE/CM,kBAAiBD,iBAAiB,QAAS,WACzCJ,cACC"}`
  );
});

const BASE_URL = `http://localhost:${httpServer.identity.primaryPort}/`;

// Tests that the pretty-print icon is visible for source files with a sourceMappingURL
// but the linked sourcemap is not found or no original files exist.
add_task(async () => {
  const dbg = await initDebuggerWithAbsoluteURL(
    `${BASE_URL}doc-prettyprint-sourcemap.html`,
    "doc-prettyprint-sourcemap.html",
    "js1.min.js",
    "js2.min.js"
  );

  info(" - Test HTML source");
  const htmlSource = findSource(dbg, "doc-prettyprint-sourcemap.html");
  await selectSource(dbg, htmlSource);
  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"),
    false
  );

  info(" - Test source with sourceMappingURL but sourcemap does not exist");
  const source1 = findSource(dbg, "js1.min.js");
  await selectSource(dbg, source1);

  // The source may be reported as pretty printable while we are fetching the sourcemap,
  // but once the sourcemap is reported to be failing, we no longer report to be pretty printable.
  await waitFor(
    () => !dbg.selectors.isSourceWithMap(source1.id),
    "Wait for the selector to report the source to be source-map less"
  );

  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"),
    false
  );

  info(
    " - Test source with sourceMappingURL and sourcemap but no original files"
  );
  const source2 = findSource(dbg, "js2.min.js");
  await selectSource(dbg, source2);

  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"),
    false
  );

  info(" - Test an already pretty-printed source");
  info("Pretty print the script");
  clickElement(dbg, "prettyPrintButton");

  await waitForSelectedSource(dbg, "js2.min.js:formatted");
  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceFooter.prettyPrint.isPrettyPrintedMessage"),
    true
  );
});

add_task(async () => {
  const dbg = await initDebugger(
    "doc-sourcemaps2.html",
    "main.min.js",
    "main.js"
  );

  info(
    " - Test source with sourceMappingURL, sourcemap and original files exist"
  );
  const source = findSource(dbg, "main.min.js");
  await selectSource(dbg, source);

  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceFooter.prettyPrint.hasSourceMapMessage"),
    true
  );

  info(" - Test an original source");
  const originalSource = findSource(dbg, "main.js");
  await selectSource(dbg, originalSource);

  assertPrettyPrintButton(
    dbg,
    DEBUGGER_L10N.getStr("sourceFooter.prettyPrint.isOriginalMessage"),
    true
  );
});

function assertPrettyPrintButton(dbg, expectedTitle, shouldBeDisabled) {
  const prettyPrintButton = findElement(dbg, "prettyPrintButton");
  ok(prettyPrintButton, "Pretty Print Button is visible");
  is(
    prettyPrintButton.title,
    expectedTitle,
    "The pretty print button title should be correct"
  );
  ok(
    shouldBeDisabled ? prettyPrintButton.disabled : !prettyPrintButton.disabled,
    `The pretty print button should be ${
      shouldBeDisabled ? "disabled" : "enabled"
    }`
  );
}