tor-browser

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

browser_dbg-pause-on-unload.js (4125B)


      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 that the debugger works when pausing on unload.
      6 
      7 "use strict";
      8 
      9 const TEST_URL_1 =
     10  "data:text/html," +
     11  encodeURI(
     12    `<script>window.addEventListener("unload", (event) => { debugger; });</script>`
     13  );
     14 
     15 add_task(async function debuggerStatementOnUnload() {
     16  const dbg = await initDebuggerWithAbsoluteURL(TEST_URL_1);
     17 
     18  await addExpression(dbg, "event.type");
     19  is(getWatchExpressionLabel(dbg, 1), "event.type");
     20  is(getWatchExpressionValue(dbg, 1), "(unavailable)");
     21 
     22  info("Reloading the page should trigger the debugger statement on unload");
     23  const evaluated = waitForDispatch(dbg.store, "EVALUATE_EXPRESSIONS");
     24  const onReload = reload(dbg);
     25 
     26  await waitForPaused(dbg);
     27  await waitForInlinePreviews(dbg);
     28  await assertPausedAtSourceAndLine(dbg, findSource(dbg, TEST_URL_1).id, 1, 56);
     29 
     30  await evaluated;
     31  is(
     32    getWatchExpressionValue(dbg, 1),
     33    `"unload"`,
     34    "event.type evaluation does return the expected result"
     35  );
     36 
     37  // Verify that project search works while being paused on unload
     38  await openProjectSearch(dbg);
     39  await doProjectSearch(dbg, "unload", 1);
     40 
     41  info("Resume execution and wait for the page to complete its reload");
     42  await resume(dbg);
     43  await onReload;
     44 });
     45 
     46 // Note that inline exception doesn't work on single line <script>
     47 const TEST_URL_2 =
     48  "data:text/html," +
     49  encodeURI(`<script>
     50 window.addEventListener("unload", () => { throw new Error("Exception on unload") });
     51 [].inlineException();
     52 </script>`);
     53 
     54 add_task(async function exceptionsOnUnload() {
     55  const dbg = await initDebuggerWithAbsoluteURL(TEST_URL_2);
     56 
     57  info("Enable pause on uncaught exception (but not for caught exception)");
     58  await togglePauseOnExceptions(dbg, true, false);
     59 
     60  await openProjectSearch(dbg);
     61  await doProjectSearch(dbg, "exception", 1);
     62  is(getExpandedResultsCount(dbg), 2);
     63 
     64  info("Reloading the page should trigger the debugger statement on unload");
     65  const onReload = reload(dbg);
     66 
     67  // Cover catching exception on unload
     68  await waitForPaused(dbg);
     69  await assertPausedAtSourceAndLine(dbg, findSource(dbg, TEST_URL_2).id, 2, 49);
     70 
     71  // But also that previous inline exceptions are still visible
     72  await assertInlineExceptionPreview(dbg, 3, 4, {
     73    fields: [["<anonymous>", TEST_URL_2 + ":3"]],
     74    result: "TypeError: [].inlineException is not a function",
     75    expression: "inlineException",
     76  });
     77 
     78  // Verify that project search results are still displayed
     79  is(getExpandedResultsCount(dbg), 2);
     80 
     81  // Stop pausing on exception to prevent pausing on the inline exception on the load of the next page
     82  await togglePauseOnExceptions(dbg, false, false);
     83 
     84  info("Resume execution and wait for the page to complete its reload");
     85  await resume(dbg);
     86  await onReload;
     87 });
     88 
     89 add_task(async function debuggerStatementOnIframeUnload() {
     90  const url = `data:text/html,<iframe src="${encodeURI(TEST_URL_1)}"></iframe>`;
     91  const dbg = await initDebuggerWithAbsoluteURL(url);
     92 
     93  await addExpression(dbg, "event.type");
     94  is(getWatchExpressionLabel(dbg, 1), "event.type");
     95  is(getWatchExpressionValue(dbg, 1), "(unavailable)");
     96 
     97  info("Removing the iframe should trigger the debugger statement on unload");
     98  const evaluated = waitForDispatch(dbg.store, "EVALUATE_EXPRESSIONS");
     99  const onResumed = SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    100    content.document.querySelector("iframe").remove();
    101  });
    102 
    103  await waitForPaused(dbg);
    104  await waitForInlinePreviews(dbg);
    105  await assertPausedAtSourceAndLine(dbg, findSource(dbg, TEST_URL_1).id, 1, 56);
    106 
    107  await evaluated;
    108  is(
    109    getWatchExpressionValue(dbg, 1),
    110    `"unload"`,
    111    "event.type evaluation does return the expected result"
    112  );
    113 
    114  // Verify that project search works while being paused on unload
    115  await openProjectSearch(dbg);
    116  await doProjectSearch(dbg, "unload", 1);
    117 
    118  info("Resume execution and wait for the page to complete its reload");
    119  await resume(dbg);
    120  await onResumed;
    121 });