tor-browser

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

browser_disableDialogs_onbeforeunload.js (1974B)


      1 const { PromptTestUtils } = ChromeUtils.importESModule(
      2  "resource://testing-common/PromptTestUtils.sys.mjs"
      3 );
      4 
      5 function pageScript() {
      6  window.addEventListener(
      7    "beforeunload",
      8    function (event) {
      9      var str = "Some text that causes the beforeunload dialog to be shown";
     10      event.returnValue = str;
     11      return str;
     12    },
     13    true
     14  );
     15 }
     16 
     17 SpecialPowers.pushPrefEnv({
     18  set: [["dom.require_user_interaction_for_beforeunload", false]],
     19 });
     20 
     21 const PAGE_URL =
     22  "data:text/html," +
     23  encodeURIComponent("<script>(" + pageScript.toSource() + ")();</script>");
     24 
     25 add_task(async function enableDialogs() {
     26  // The onbeforeunload dialog should appear.
     27  let dialogPromise = PromptTestUtils.waitForPrompt(null, {
     28    modalType: Services.prompt.MODAL_TYPE_CONTENT,
     29    promptType: "confirmEx",
     30  });
     31 
     32  let openPagePromise = openPage(true);
     33  let dialog = await dialogPromise;
     34  Assert.ok(true, "Showed the beforeunload dialog.");
     35 
     36  await PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 0 });
     37  await openPagePromise;
     38 });
     39 
     40 add_task(async function disableDialogs() {
     41  // The onbeforeunload dialog should NOT appear.
     42  await openPage(false);
     43  info("If we time out here, then the dialog was shown...");
     44 });
     45 
     46 async function openPage(enableDialogs) {
     47  // Open about:blank in a new tab.
     48  await BrowserTestUtils.withNewTab(
     49    { gBrowser, url: "about:blank" },
     50    async function (browser) {
     51      // Load the page.
     52      BrowserTestUtils.startLoadingURIString(browser, PAGE_URL);
     53      await BrowserTestUtils.browserLoaded(browser);
     54      // Load the content script in the frame.
     55      let methodName = enableDialogs ? "enableDialogs" : "disableDialogs";
     56      await SpecialPowers.spawn(browser, [methodName], async function (name) {
     57        content.windowUtils[name]();
     58      });
     59      // And then navigate away.
     60      BrowserTestUtils.startLoadingURIString(browser, "http://example.com/");
     61      await BrowserTestUtils.browserLoaded(browser);
     62    }
     63  );
     64 }