tor-browser

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

browser_dbg-overrides.js (4205B)


      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 /**
      6 * Testing the script overrides feature
      7 */
      8 
      9 "use strict";
     10 
     11 const httpServer = createTestHTTPServer();
     12 const BASE_URL = `http://localhost:${httpServer.identity.primaryPort}/`;
     13 
     14 httpServer.registerContentType("html", "text/html");
     15 httpServer.registerContentType("js", "application/javascript");
     16 
     17 const testSourceContent = `function foo() {
     18  console.log("original test script");
     19 }`;
     20 
     21 const testOverrideSourceContent = `function foo() {
     22  console.log("overriden test script");
     23 }
     24 foo();
     25 `;
     26 
     27 httpServer.registerPathHandler("/index.html", (request, response) => {
     28  response.setStatusLine(request.httpVersion, 200, "OK");
     29  response.write(`<!DOCTYPE html>
     30  <html>
     31    <head>
     32    <script type="text/javascript" src="/test.js"></script>
     33    </head>
     34    <body>
     35    </body>
     36  </html>
     37  `);
     38 });
     39 
     40 httpServer.registerPathHandler("/test.js", (request, response) => {
     41  response.setHeader("Content-Type", "application/javascript");
     42  response.write(testSourceContent);
     43 });
     44 
     45 add_task(async function () {
     46  const dbg = await initDebuggerWithAbsoluteURL(
     47    BASE_URL + "index.html",
     48    "test.js"
     49  );
     50 
     51  await waitForSourcesInSourceTree(dbg, ["test.js"], {
     52    noExpand: false,
     53  });
     54 
     55  let overrides = [
     56    ...findAllElementsWithSelector(dbg, ".has-network-override"),
     57  ];
     58  is(overrides.length, 0, "No override is displayed in the debugger");
     59 
     60  info("Load and assert the content of the test.js script");
     61  await selectSourceFromSourceTree(dbg, "test.js");
     62  is(
     63    getEditorContent(dbg),
     64    testSourceContent,
     65    "The test.js is the original source content"
     66  );
     67 
     68  info("Add a breakpoint");
     69  await addBreakpoint(dbg, "test.js", 2);
     70 
     71  info("Select test.js tree node, and add override");
     72  const MockFilePicker = SpecialPowers.MockFilePicker;
     73  MockFilePicker.init(window.browsingContext);
     74  const nsiFile = new FileUtils.File(
     75    PathUtils.join(PathUtils.tempDir, "test.js")
     76  );
     77  MockFilePicker.setFiles([nsiFile]);
     78  const path = nsiFile.path;
     79 
     80  await triggerSourceTreeContextMenu(
     81    dbg,
     82    findSourceNodeWithText(dbg, "test.js"),
     83    "#node-menu-overrides"
     84  );
     85 
     86  info("Wait for `test.js` to be saved to disk and re-write it");
     87  await BrowserTestUtils.waitForCondition(() => IOUtils.exists(path));
     88  await BrowserTestUtils.waitForCondition(async () => {
     89    const { size } = await IOUtils.stat(path);
     90    return size > 0;
     91  });
     92 
     93  await IOUtils.write(
     94    path,
     95    new TextEncoder().encode(testOverrideSourceContent)
     96  );
     97 
     98  info("Reload and assert `test.js` pause and it overriden source content");
     99  const onReloaded = reload(dbg, "test.js");
    100  await waitForPaused(dbg);
    101 
    102  await assertPausedAtSourceAndLine(dbg, findSource(dbg, "test.js").id, 2);
    103  is(
    104    getEditorContent(dbg),
    105    testOverrideSourceContent,
    106    "The test.js is the overridden source content"
    107  );
    108 
    109  await resume(dbg);
    110  await onReloaded;
    111 
    112  info("Check that an override icon is displayed in the source tree");
    113  overrides = [...findAllElementsWithSelector(dbg, ".has-network-override")];
    114  is(overrides.length, 1, "One override should be displayed in the debugger");
    115 
    116  const otherDebugger = await initDebuggerWithAbsoluteURL(
    117    BASE_URL + "index.html",
    118    "test.js"
    119  );
    120  await waitForSourcesInSourceTree(otherDebugger, ["test.js"], {
    121    noExpand: false,
    122  });
    123  overrides = [
    124    ...findAllElementsWithSelector(otherDebugger, ".has-network-override"),
    125  ];
    126  is(overrides.length, 0, "No override is displayed in the other debugger");
    127  await closeToolboxAndTab(otherDebugger.toolbox);
    128 
    129  info("Remove override and test");
    130  const removed = waitForDispatch(dbg.toolbox.store, "REMOVE_NETWORK_OVERRIDE");
    131  await triggerSourceTreeContextMenu(
    132    dbg,
    133    findSourceNodeWithText(dbg, "test.js"),
    134    "#node-menu-overrides"
    135  );
    136  await removed;
    137 
    138  info("Reload and assert `test.js`'s original source content");
    139  await reload(dbg, "test.js");
    140  await waitForSelectedSource(dbg, "test.js");
    141 
    142  is(
    143    getEditorContent(dbg),
    144    testSourceContent,
    145    "The test.js is the original source content"
    146  );
    147 });