tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_dbg-pretty-print.js (4634B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 // Tests basic pretty-printing functionality.
      6 
      7 "use strict";
      8 
      9 requestLongerTimeout(2);
     10 
     11 add_task(async function () {
     12  const dbg = await initDebugger("doc-minified.html", "math.min.js");
     13 
     14  await selectSource(dbg, "math.min.js", 2);
     15  await togglePrettyPrint(dbg);
     16  is(
     17    countTabs(dbg),
     18    1,
     19    "After pretty printing there is still only one tab in the reducer store"
     20  );
     21 
     22  ok(
     23    !findElement(dbg, "mappedSourceLink"),
     24    "When we are on the pretty printed source, we don't show the link to the minified source"
     25  );
     26  const footerButton = findElement(dbg, "sourceMapFooterButton");
     27  ok(
     28    !footerButton.classList.contains("original"),
     29    "The pretty printed source isn't described as an original to the user"
     30  );
     31  ok(
     32    footerButton.classList.contains("not-mapped"),
     33    "Neither is it described as being mapped"
     34  );
     35  const ppSrc = findSource(dbg, "math.min.js:formatted");
     36 
     37  ok(ppSrc, "Pretty-printed source exists");
     38 
     39  // this is not implemented yet
     40  // assertHighlightLocation(dbg, "math.min.js:formatted", 18);
     41  // await selectSource(dbg, "math.min.js")
     42  await addBreakpoint(dbg, ppSrc, 18);
     43 
     44  invokeInTab("arithmetic");
     45  await waitForPaused(dbg);
     46 
     47  await assertPausedAtSourceAndLine(dbg, ppSrc.id, 18);
     48 
     49  await stepOver(dbg);
     50 
     51  await assertPausedAtSourceAndLine(dbg, ppSrc.id, 39);
     52 
     53  await resume(dbg);
     54 
     55  ok(
     56    !findElement(dbg, "prettyPrintButton").disabled,
     57    "Pretty Print Button shouldn't be disabled when on the pretty print version"
     58  );
     59  ok(
     60    !findElement(dbg, "prettyPrintButton").classList.contains("active"),
     61    "Pretty Print Button should be active when on the pretty print version"
     62  );
     63 
     64  // Ensure that trying to select the minified version, would select the pretty printed one
     65  // Do not wait for `selectSource` as it won't select the expected source for the test helper
     66  selectSource(dbg, "math.min.js");
     67  await waitForSelectedSource(dbg, "math.min.js:formatted");
     68  ok(
     69    !findElement(dbg, "mappedSourceLink"),
     70    "When we are on the minified source,  we don't show the link to the pretty printed source"
     71  );
     72 
     73  ok(
     74    !findElement(dbg, "prettyPrintButton").disabled,
     75    "Pretty Print Button should be enabled"
     76  );
     77 
     78  await togglePrettyPrint(dbg);
     79  is(
     80    countTabs(dbg),
     81    1,
     82    "After disabling pretty printing, there is still only one tab in the reducer store"
     83  );
     84 });
     85 
     86 add_task(async function testPrivateFields() {
     87  // Create a source containing a class with private fields
     88  const httpServer = createTestHTTPServer();
     89  httpServer.registerContentType("html", "text/html");
     90  httpServer.registerContentType("js", "application/javascript");
     91 
     92  httpServer.registerPathHandler(`/`, function (request, response) {
     93    response.setStatusLine(request.httpVersion, 200, "OK");
     94    response.write(`
     95      <html>
     96          Test pretty-printing class with private fields
     97          <script type="text/javascript" src="class-with-private-fields.js"></script>
     98      </html>`);
     99  });
    100 
    101  httpServer.registerPathHandler(
    102    "/class-with-private-fields.js",
    103    function (request, response) {
    104      response.setHeader("Content-Type", "application/javascript");
    105      response.write(`
    106      class MyClass {
    107        constructor(a) {
    108          this.#a = a;this.#b = Math.random();this.ab = this.#getAB();
    109        }
    110        #a
    111        #b = "default value"
    112        static #someStaticPrivate
    113        #getA() {
    114          return this.#a;
    115        }
    116        #getAB() {
    117          return this.#getA()+this.
    118            #b
    119        }
    120      }
    121  `);
    122    }
    123  );
    124  const port = httpServer.identity.primaryPort;
    125  const TEST_URL = `http://localhost:${port}/`;
    126 
    127  info("Open toolbox");
    128  const dbg = await initDebuggerWithAbsoluteURL(TEST_URL);
    129 
    130  info("Select script with private fields");
    131  await selectSource(dbg, "class-with-private-fields.js", 2);
    132 
    133  info("Pretty print the script");
    134  await togglePrettyPrint(dbg);
    135 
    136  info("Check that the script was pretty-printed as expected");
    137  const prettyPrintedSource = await findSourceContent(
    138    dbg,
    139    "class-with-private-fields.js:formatted"
    140  );
    141 
    142  is(
    143    prettyPrintedSource.value.trim(),
    144    `
    145 class MyClass {
    146  constructor(a) {
    147    this.#a = a;
    148    this.#b = Math.random();
    149    this.ab = this.#getAB();
    150  }
    151  #a
    152  #b = 'default value'
    153  static #someStaticPrivate
    154  #getA() {
    155    return this.#a;
    156  }
    157  #getAB() {
    158    return this.#getA() + this.#b
    159  }
    160 }
    161  `.trim(),
    162    "script was pretty printed as expected"
    163  );
    164 });