tor-browser

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

browser_tabdialogbox_navigation.js (5262B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TEST_ROOT_CHROME = getRootDirectory(gTestPath);
      7 const TEST_DIALOG_PATH = TEST_ROOT_CHROME + "subdialog.xhtml";
      8 
      9 /**
     10 * Tests that all tab dialogs are closed on navigation.
     11 */
     12 add_task(async function test_tabdialogbox_multiple_close_on_nav() {
     13  await BrowserTestUtils.withNewTab(
     14    "https://example.com",
     15    async function (browser) {
     16      // Open two dialogs and wait for them to be ready.
     17      let dialogBox = gBrowser.getTabDialogBox(browser);
     18      let closedPromises = [
     19        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
     20        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
     21      ];
     22 
     23      let dialogs = dialogBox.getTabDialogManager()._dialogs;
     24 
     25      is(dialogs.length, 2, "Dialog manager has two dialogs.");
     26 
     27      info("Waiting for dialogs to open.");
     28      await Promise.all(dialogs.map(dialog => dialog._dialogReady));
     29 
     30      // Navigate to a different page
     31      BrowserTestUtils.startLoadingURIString(browser, "https://example.org");
     32 
     33      info("Waiting for dialogs to close.");
     34      await closedPromises;
     35 
     36      ok(true, "All open dialogs should close on navigation");
     37    }
     38  );
     39 });
     40 
     41 /**
     42 * Tests dialog close on navigation triggered by web content.
     43 */
     44 add_task(async function test_tabdialogbox_close_on_content_nav() {
     45  await BrowserTestUtils.withNewTab(
     46    "https://example.com",
     47    async function (browser) {
     48      // Open a dialog and wait for it to be ready
     49      let dialogBox = gBrowser.getTabDialogBox(browser);
     50      let { closedPromise } = dialogBox.open(TEST_DIALOG_PATH);
     51 
     52      let dialog = dialogBox.getTabDialogManager()._topDialog;
     53 
     54      is(
     55        dialogBox.getTabDialogManager()._dialogs.length,
     56        1,
     57        "Dialog manager has one dialog."
     58      );
     59 
     60      info("Waiting for dialog to open.");
     61      await dialog._dialogReady;
     62 
     63      // Trigger a same origin navigation by the content
     64      await ContentTask.spawn(browser, {}, () => {
     65        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     66        content.location = "http://example.com/1";
     67      });
     68 
     69      info("Waiting for dialog to close.");
     70      await closedPromise;
     71      ok(
     72        true,
     73        "Dialog should close for same origin navigation by the content."
     74      );
     75 
     76      // Open a new dialog
     77      closedPromise = dialogBox.open(TEST_DIALOG_PATH, {
     78        keepOpenSameOriginNav: true,
     79      }).closedPromise;
     80 
     81      info("Waiting for dialog to open.");
     82      await dialog._dialogReady;
     83 
     84      SimpleTest.requestFlakyTimeout("Waiting to ensure dialog does not close");
     85      let race = Promise.race([
     86        closedPromise,
     87        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     88        new Promise(resolve => setTimeout(() => resolve("success"), 1000)),
     89      ]);
     90 
     91      // Trigger a same origin navigation by the content
     92      await ContentTask.spawn(browser, {}, () => {
     93        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     94        content.location = "http://example.com/test";
     95      });
     96 
     97      is(
     98        await race,
     99        "success",
    100        "Dialog should not close for same origin navigation by the content."
    101      );
    102 
    103      // Trigger a cross origin navigation by the content
    104      await ContentTask.spawn(browser, {}, () => {
    105        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    106        content.location = "http://example.org/test2";
    107      });
    108 
    109      info("Waiting for dialog to close");
    110      await closedPromise;
    111 
    112      ok(
    113        true,
    114        "Dialog should close for cross origin navigation by the content."
    115      );
    116    }
    117  );
    118 });
    119 
    120 /**
    121 * Hides a dialog stack and tests that behavior doesn't change. Ensures
    122 * navigation triggered by web content still closes all dialogs.
    123 */
    124 add_task(async function test_tabdialogbox_hide() {
    125  await BrowserTestUtils.withNewTab(
    126    "https://example.com",
    127    async function (browser) {
    128      // Open a dialog and wait for it to be ready
    129      let dialogBox = gBrowser.getTabDialogBox(browser);
    130      let dialogBoxManager = dialogBox.getTabDialogManager();
    131      let closedPromises = [
    132        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
    133        dialogBox.open(TEST_DIALOG_PATH).closedPromise,
    134      ];
    135 
    136      let dialogs = dialogBox.getTabDialogManager()._dialogs;
    137 
    138      is(
    139        dialogBox.getTabDialogManager()._dialogs.length,
    140        2,
    141        "Dialog manager has two dialogs."
    142      );
    143 
    144      info("Waiting for dialogs to open.");
    145      await Promise.all(dialogs.map(dialog => dialog._dialogReady));
    146 
    147      ok(
    148        !BrowserTestUtils.isHidden(dialogBoxManager._dialogStack),
    149        "Dialog stack is showing"
    150      );
    151 
    152      dialogBoxManager.hideDialog(browser);
    153 
    154      is(
    155        dialogBoxManager._dialogs.length,
    156        2,
    157        "Dialog manager still has two dialogs."
    158      );
    159 
    160      ok(
    161        BrowserTestUtils.isHidden(dialogBoxManager._dialogStack),
    162        "Dialog stack is hidden"
    163      );
    164 
    165      // Navigate to a different page
    166      BrowserTestUtils.startLoadingURIString(browser, "https://example.org");
    167 
    168      info("Waiting for dialogs to close.");
    169      await closedPromises;
    170 
    171      ok(true, "All open dialogs should still close on navigation");
    172    }
    173  );
    174 });