tor-browser

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

browser_notification_tab_switching.js (3912B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 "use strict";
      6 
      7 const { PermissionTestUtils } = ChromeUtils.importESModule(
      8  "resource://testing-common/PermissionTestUtils.sys.mjs"
      9 );
     10 
     11 var tab;
     12 var notification;
     13 var notificationURL =
     14  "https://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html";
     15 var newWindowOpenedFromTab;
     16 
     17 add_task(async function test_notificationPreventDefaultAndSwitchTabs() {
     18  await addNotificationPermission(notificationURL);
     19 
     20  let originalTab = gBrowser.selectedTab;
     21  await BrowserTestUtils.withNewTab(
     22    {
     23      gBrowser,
     24      url: notificationURL,
     25    },
     26    async function dummyTabTask(aBrowser) {
     27      // Put new tab in background so it is obvious when it is re-focused.
     28      await BrowserTestUtils.switchTab(gBrowser, originalTab);
     29      isnot(
     30        gBrowser.selectedBrowser,
     31        aBrowser,
     32        "Notification page loaded as a background tab"
     33      );
     34 
     35      // First, show a notification that will be have the tab-switching prevented.
     36      function promiseNotificationEvent(evt) {
     37        return SpecialPowers.spawn(
     38          aBrowser,
     39          [evt],
     40          async function (contentEvt) {
     41            return new Promise(resolve => {
     42              let contentNotification = content.wrappedJSObject._notification;
     43              contentNotification.addEventListener(
     44                contentEvt,
     45                function (event) {
     46                  resolve({ defaultPrevented: event.defaultPrevented });
     47                },
     48                { once: true }
     49              );
     50            });
     51          }
     52        );
     53      }
     54      await openNotification(aBrowser, "showNotification1");
     55      info("Notification alert showing");
     56      let alertWindow = Services.wm.getMostRecentWindow("alert:alert");
     57      if (!alertWindow) {
     58        ok(true, "Notifications don't use XUL windows on all platforms.");
     59        await closeNotification(aBrowser);
     60        return;
     61      }
     62      info("Clicking on notification");
     63      let promiseClickEvent = promiseNotificationEvent("click");
     64 
     65      // NB: This executeSoon is needed to allow the non-e10s runs of this test
     66      // a chance to set the event listener on the page. Otherwise, we
     67      // synchronously fire the click event before we listen for the event.
     68      executeSoon(() => {
     69        EventUtils.synthesizeMouseAtCenter(
     70          alertWindow.document.getElementById("alertTitleLabel"),
     71          {},
     72          alertWindow
     73        );
     74      });
     75      let clickEvent = await promiseClickEvent;
     76      ok(
     77        clickEvent.defaultPrevented,
     78        "The event handler for the first notification cancels the event"
     79      );
     80      isnot(
     81        gBrowser.selectedBrowser,
     82        aBrowser,
     83        "Notification page still a background tab"
     84      );
     85      let notificationClosed = promiseNotificationEvent("close");
     86      await closeNotification(aBrowser);
     87      await notificationClosed;
     88 
     89      // Second, show a notification that will cause the tab to get switched.
     90      await openNotification(aBrowser, "showNotification2");
     91      alertWindow = Services.wm.getMostRecentWindow("alert:alert");
     92      let promiseTabSelect = BrowserTestUtils.waitForEvent(
     93        gBrowser.tabContainer,
     94        "TabSelect"
     95      );
     96      EventUtils.synthesizeMouseAtCenter(
     97        alertWindow.document.getElementById("alertTitleLabel"),
     98        {},
     99        alertWindow
    100      );
    101      await promiseTabSelect;
    102      is(
    103        gBrowser.selectedBrowser.currentURI.spec,
    104        notificationURL,
    105        "Clicking on the second notification should select its originating tab"
    106      );
    107      notificationClosed = promiseNotificationEvent("close");
    108      await closeNotification(aBrowser);
    109      await notificationClosed;
    110    }
    111  );
    112 });
    113 
    114 add_task(async function cleanup() {
    115  PermissionTestUtils.remove(notificationURL, "desktop-notification");
    116 });