tor-browser

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

browser_dbg-breakpoints-reloading-with-source-changes.js (5814B)


      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 // This tests breakpoints resyncing when source content changes
      6 // after reload.
      7 "use strict";
      8 
      9 const httpServer = createTestHTTPServer();
     10 httpServer.registerContentType("html", "text/html");
     11 httpServer.registerContentType("js", "application/javascript");
     12 
     13 httpServer.registerPathHandler(
     14  "/doc-breakpoint-reload.html",
     15  (request, response) => {
     16    response.setStatusLine(request.httpVersion, 200, "OK");
     17    response.write(`<!DOCTYPE html>
     18    <html>
     19      <head>
     20        <script type="text/javascript" src="/script.js"></script>
     21      </head>
     22    </html>
     23  `);
     24  }
     25 );
     26 
     27 let views = 0;
     28 httpServer.registerPathHandler("/script.js", (request, response) => {
     29  response.setHeader("Content-Type", "application/javascript");
     30  // The script contents to serve on reload of script.js. Each relaod
     31  // cycles through the script content.
     32  const content = [
     33    // CONTENT 1: Source content with 1 function
     34    // The breakpoint will be set on line 3 (i.e with the `return` statement)
     35    `function bar() {
     36  const prefix = "long";
     37  return prefix + "bar";
     38 }
     39 console.log(bar());`,
     40 
     41    // CONTENT 2: Source content with 2 functions, where the breakpoint is now in a
     42    // different function though the line does not change.
     43    `function foo() {
     44  const prefix = "long";
     45  return prefix + "foo";
     46 }
     47 
     48 function bar() {
     49  const prefix = "long";
     50  return prefix + "bar";
     51 }
     52 console.log(bar(), foo());`,
     53 
     54    // CONTENT 3: Source content with comments and 1 function, where the breakpoint is
     55    // is now at a line with comments (non-breakable line).
     56    `// This is a random comment which is here
     57 // to move the function a couple of lines
     58 // down, making sure the breakpoint is on
     59 // a non-breakable line.
     60 function bar() {
     61  const prefix = "long";
     62  return prefix + "bar";
     63 }
     64 console.log(bar());`,
     65 
     66    // CONTENT 4: Source content with just a comment where the line which the breakpoint
     67    // is supposed to be on no longer exists.
     68    `// one line comment`,
     69  ];
     70 
     71  response.write(content[views % content.length]);
     72  views++;
     73 });
     74 
     75 const BASE_URL = `http://localhost:${httpServer.identity.primaryPort}/`;
     76 
     77 add_task(async function testBreakpointInFunctionRelocation() {
     78  info("Start test for relocation of breakpoint set in a function");
     79  const dbg = await initDebuggerWithAbsoluteURL(
     80    `${BASE_URL}doc-breakpoint-reload.html`,
     81    "script.js"
     82  );
     83 
     84  let source = findSource(dbg, "script.js");
     85  await selectSource(dbg, source);
     86 
     87  info("Add breakpoint in bar()");
     88  await addBreakpoint(dbg, source, 3);
     89 
     90  info(
     91    "Assert the text content on line 3 to make sure the breakpoint was set in bar()"
     92  );
     93  assertTextContentOnLine(dbg, 3, 'return prefix + "bar";');
     94 
     95  info("Check that only one breakpoint is set in the reducer");
     96  is(dbg.selectors.getBreakpointCount(), 1, "Only one breakpoint exists");
     97 
     98  info("Check that only one breakpoint is set on the server");
     99  is(
    100    dbg.client.getServerBreakpointsList().length,
    101    1,
    102    "Only one breakpoint exists on the server"
    103  );
    104 
    105  info(
    106    "Reload should change the source content to CONTENT 2 i.e 2 functions foo() and bar()"
    107  );
    108  const onReloaded = reload(dbg);
    109  await waitForPaused(dbg);
    110 
    111  source = findSource(dbg, "script.js");
    112 
    113  info("Assert that the breakpoint pauses on line 3");
    114  assertPausedAtSourceAndLine(dbg, source.id, 3);
    115 
    116  info("Assert that the breakpoint is visible on line 3");
    117  await assertBreakpoint(dbg, 3);
    118 
    119  info("Assert the text content on line 3 to make sure we are paused in foo()");
    120  assertTextContentOnLine(dbg, 3, 'return prefix + "foo";');
    121 
    122  info("Check that only one breakpoint currently exists in the reducer");
    123  is(dbg.selectors.getBreakpointCount(), 1, "One breakpoint exists");
    124 
    125  info("Check that only one breakpoint exist on the server");
    126  is(
    127    dbg.client.getServerBreakpointsList().length,
    128    1,
    129    "Only one breakpoint exists on the server"
    130  );
    131 
    132  await resume(dbg);
    133  info("Wait for reload to complete after resume");
    134  await onReloaded;
    135 
    136  info(
    137    "Reload should change the source content to CONTENT 3 i.e comments and 1 function bar()"
    138  );
    139  await reload(dbg);
    140  await waitForSelectedSource(dbg, "script.js");
    141 
    142  await assertNotPaused(dbg);
    143 
    144  info(
    145    "Assert that the breakpoint is not visible on line 3 which is a non-breakable line"
    146  );
    147  await assertNoBreakpoint(dbg, 3);
    148 
    149  info("Check that no breakpoint exists in the reducer");
    150  is(dbg.selectors.getBreakpointCount(), 0, "Breakpoint has been removed");
    151 
    152  // See comment on line 175
    153  info("Check that one breakpoint exists on the server");
    154  is(
    155    dbg.client.getServerBreakpointsList().length,
    156    1,
    157    "Breakpoint has been removed on the server"
    158  );
    159 
    160  info(
    161    "Reload should change the source content to CONTENT 4 which is just a one comment line"
    162  );
    163  await reload(dbg);
    164  await waitForSelectedSource(dbg, "script.js");
    165 
    166  // There will initially be zero breakpoints, but wait to make sure none are
    167  // installed while syncing.
    168  await wait(1000);
    169  assertNotPaused(dbg);
    170 
    171  info("Assert that the source content is one comment line");
    172  assertTextContentOnLine(dbg, 1, "// one line comment");
    173 
    174  info("Check that no breakpoint still exists in the reducer");
    175  is(dbg.selectors.getBreakpointCount(), 0, "No breakpoint exists");
    176 
    177  // Breakpoints do not get removed in this situation, to support breakpoints in
    178  // inline sources which load and get hit progressively. See the browser_dbg-breakpoints-reloading.js
    179  // test for this behaviour.
    180  info("Check that one breakpoint exists on the server");
    181  is(
    182    dbg.client.getServerBreakpointsList().length,
    183    1,
    184    "One breakpoint exists on the server"
    185  );
    186 });