tor-browser

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

browser_popupNotification_swapBrowser.js (4302B)


      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_SECURITY_DELAY = 1;
      7 const TEST_WINDOW_TIME_OPEN = 100;
      8 
      9 add_setup(async function () {
     10  // Set the lowest non-zero security delay for PopupNotification actions so we can test
     11  // if the delay causes inability to press the buttons on PopupNotifications when windows
     12  // are swapped
     13  // Geo Timeout Pref is set to 0, to ensure that the test does not wait location provider
     14  // to start
     15  await SpecialPowers.pushPrefEnv({
     16    set: [
     17      ["security.notification_enable_delay", TEST_SECURITY_DELAY],
     18      ["geo.timeout", 0],
     19    ],
     20  });
     21 });
     22 
     23 // Function to check if Popup Notifications has been shown
     24 function waitForNotificationPanel(window, callback) {
     25  let listener = event => {
     26    if (event.target != window.PopupNotifications.panel) {
     27      return;
     28    }
     29    window.PopupNotifications.panel.removeEventListener("popupshown", listener);
     30    gActiveListeners.delete(listener);
     31    executeSoon(() => callback.call(window.PopupNotifications.panel));
     32  };
     33  gActiveListeners.set(listener, "popupshown");
     34  window.PopupNotifications.panel.addEventListener("popupshown", listener);
     35 }
     36 
     37 // Testing that the Popup Notification security delay is compared to the
     38 // global process timer instead of the window specific process timer
     39 // as if it is based on the window specific process timer and the tab
     40 // with the Popup Notification is moved to another window with a lower
     41 // window specific process counter, it would block the user from interacting
     42 // with the buttons on the panel because of the security delay
     43 add_task(async function transferPopupNotificationToNewWindowAndResolve() {
     44  await ensureSecurityDelayReady();
     45 
     46  let window = await BrowserTestUtils.openNewBrowserWindow();
     47  let tab = await BrowserTestUtils.openNewForegroundTab(
     48    window.gBrowser,
     49    "https://test1.example.com/"
     50  );
     51 
     52  let browser = tab.linkedBrowser;
     53 
     54  // This timeout is used simulate the a window being open for an extended period of
     55  // time before a Popup Notification is shown so that when the tab containing the
     56  // Popup Notification is moved to a new window there is large enough difference
     57  // between the initial windows interal timer and the new windows interal timer so
     58  // that it would impact the security delay if it was based on the windows interal timer
     59  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     60  await new Promise(resolve => setTimeout(resolve, TEST_WINDOW_TIME_OPEN));
     61 
     62  // Open Geolocation popup
     63  let popupShownPromise = new Promise(resolve => {
     64    waitForNotificationPanel(window, function () {
     65      resolve(this);
     66    });
     67  });
     68  await SpecialPowers.spawn(browser, [], async function () {
     69    content.navigator.geolocation.getCurrentPosition(() => {});
     70  });
     71  await popupShownPromise;
     72 
     73  let notification = window.PopupNotifications.getNotification("geolocation");
     74  ok(
     75    window.PopupNotifications.isPanelOpen && notification,
     76    "Geolocation notification is open"
     77  );
     78 
     79  // Move Tab with Popup Notification to a new window with its own
     80  // performance.now() counter
     81  let promiseWin = BrowserTestUtils.waitForNewWindow();
     82  let newWindow = window.gBrowser.replaceTabWithWindow(tab);
     83  await promiseWin;
     84  popupShownPromise = new Promise(resolve => {
     85    waitForNotificationPanel(newWindow, function () {
     86      resolve(this);
     87    });
     88  });
     89  await waitForWindowReadyForPopupNotifications(newWindow);
     90  await popupShownPromise;
     91  let timeNow = ChromeUtils.now();
     92 
     93  // Ensure security delay is completed
     94  await ensureSecurityDelayReady(timeNow);
     95 
     96  // Ensure Popup is still open
     97  ok(
     98    newWindow.PopupNotifications.isPanelOpen,
     99    "Geolocation notification is open"
    100  );
    101 
    102  let popupHidden = BrowserTestUtils.waitForEvent(
    103    newWindow.PopupNotifications.panel,
    104    "popuphidden"
    105  );
    106 
    107  // Attempt to resolve the Popup
    108  let acceptBtn = newWindow.PopupNotifications.panel.querySelector(
    109    ".popup-notification-primary-button"
    110  );
    111  acceptBtn.click();
    112 
    113  await popupHidden;
    114  // Esnure the Popup has been resolved
    115  Assert.ok(
    116    !newWindow.PopupNotifications.isPanelOpen,
    117    "Geolocation popup is hidden"
    118  );
    119 
    120  await BrowserTestUtils.closeWindow(newWindow);
    121  await BrowserTestUtils.closeWindow(window);
    122 });