tor-browser

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

browser_notification_close.js (2398B)


      1 "use strict";
      2 
      3 const { PlacesTestUtils } = ChromeUtils.importESModule(
      4  "resource://testing-common/PlacesTestUtils.sys.mjs"
      5 );
      6 
      7 const { PermissionTestUtils } = ChromeUtils.importESModule(
      8  "resource://testing-common/PermissionTestUtils.sys.mjs"
      9 );
     10 
     11 let notificationURL =
     12  "https://example.org/browser/browser/base/content/test/alerts/file_dom_notifications.html";
     13 
     14 add_task(async function test_notificationClose() {
     15  await addNotificationPermission(notificationURL);
     16 
     17  await BrowserTestUtils.withNewTab(
     18    {
     19      gBrowser,
     20      url: notificationURL,
     21    },
     22    async function dummyTabTask(aBrowser) {
     23      await openNotification(aBrowser, "showNotification2");
     24 
     25      info("Notification alert showing");
     26 
     27      let alertWindow = Services.wm.getMostRecentWindow("alert:alert");
     28      if (!alertWindow) {
     29        ok(true, "Notifications don't use XUL windows on all platforms.");
     30        await closeNotification(aBrowser);
     31        return;
     32      }
     33 
     34      let alertTitleLabel =
     35        alertWindow.document.getElementById("alertTitleLabel");
     36      is(
     37        alertTitleLabel.value,
     38        "Test title",
     39        "Title text of notification should be present"
     40      );
     41      let alertTextLabel =
     42        alertWindow.document.getElementById("alertTextLabel");
     43      is(
     44        alertTextLabel.textContent,
     45        "Test body 2",
     46        "Body text of notification should be present"
     47      );
     48 
     49      let alertCloseButton = alertWindow.document.querySelector(".close-icon");
     50      is(alertCloseButton.localName, "toolbarbutton", "close button found");
     51      let promiseBeforeUnloadEvent = BrowserTestUtils.waitForEvent(
     52        alertWindow,
     53        "beforeunload"
     54      );
     55      let closedTime = alertWindow.Date.now();
     56      alertCloseButton.click();
     57      info("Clicked on close button");
     58      await promiseBeforeUnloadEvent;
     59 
     60      ok(true, "Alert should close when the close button is clicked");
     61      let currentTime = alertWindow.Date.now();
     62      // The notification will self-close at 12 seconds, so this checks
     63      // that the notification closed before the timeout.
     64      Assert.less(
     65        currentTime - closedTime,
     66        5000,
     67        "Close requested at " +
     68          closedTime +
     69          ", actually closed at " +
     70          currentTime
     71      );
     72    }
     73  );
     74 });
     75 
     76 add_task(async function cleanup() {
     77  PermissionTestUtils.remove(notificationURL, "desktop-notification");
     78 });