tor-browser

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

head.js (4345B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 var gActiveListeners = {};
      7 
      8 loadScript("dom/quota/test/common/browser.js");
      9 
     10 function loadScript(path) {
     11  // "chrome://mochitests/content/browser/" corresponds to "TEST_HARNESS_FILES.testing.mochitest.browser.dom.quota.test.common" in moz.build,
     12  // and "_tests/testing/mochitest/browser/dom/quota/test/common" in the obj directory.
     13  const url = new URL(path, "chrome://mochitests/content/browser/");
     14  Services.scriptloader.loadSubScript(url, this);
     15 }
     16 
     17 // These event (un)registration handlers only work for one window, DONOT use
     18 // them with multiple windows.
     19 
     20 function registerPopupEventHandler(eventName, callback, win) {
     21  if (!win) {
     22    win = window;
     23  }
     24  gActiveListeners[eventName] = function (event) {
     25    if (event.target != win.PopupNotifications.panel) {
     26      return;
     27    }
     28    win.PopupNotifications.panel.removeEventListener(
     29      eventName,
     30      gActiveListeners[eventName]
     31    );
     32    delete gActiveListeners[eventName];
     33 
     34    callback.call(win.PopupNotifications.panel);
     35  };
     36  win.PopupNotifications.panel.addEventListener(
     37    eventName,
     38    gActiveListeners[eventName]
     39  );
     40 }
     41 
     42 function unregisterAllPopupEventHandlers(win) {
     43  if (!win) {
     44    win = window;
     45  }
     46  for (let eventName in gActiveListeners) {
     47    win.PopupNotifications.panel.removeEventListener(
     48      eventName,
     49      gActiveListeners[eventName]
     50    );
     51  }
     52  gActiveListeners = {};
     53 }
     54 
     55 function triggerMainCommand(popup, win) {
     56  if (!win) {
     57    win = window;
     58  }
     59  info("triggering main command");
     60  let notifications = popup.childNodes;
     61  ok(notifications.length, "at least one notification displayed");
     62  let notification = notifications[0];
     63  info("triggering command: " + notification.getAttribute("buttonlabel"));
     64 
     65  EventUtils.synthesizeMouseAtCenter(notification.button, {}, win);
     66 }
     67 
     68 async function triggerSecondaryCommand(popup, remember = false, win = window) {
     69  info("triggering secondary command");
     70  let notifications = popup.childNodes;
     71  ok(notifications.length, "at least one notification displayed");
     72  let notification = notifications[0];
     73 
     74  if (remember) {
     75    notification.checkbox.checked = true;
     76  }
     77 
     78  await EventUtils.synthesizeMouseAtCenter(
     79    notification.secondaryButton,
     80    {},
     81    win
     82  );
     83 }
     84 
     85 function dismissNotification(popup, win = window) {
     86  info("dismissing notification");
     87  executeSoon(function () {
     88    EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
     89  });
     90 }
     91 
     92 function waitForMessage(aMessage, browser) {
     93  // We cannot capture aMessage inside the checkFn, so we override the
     94  // checkFn.toSource to tunnel aMessage instead.
     95  let checkFn = function () {};
     96  checkFn.toSource = function () {
     97    return `function checkFn(event) {
     98      let message = ${aMessage.toSource()};
     99      if (event.data == message) {
    100        return true;
    101      }
    102      throw new Error(
    103       \`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
    104      );
    105    }`;
    106  };
    107 
    108  return BrowserTestUtils.waitForContentEvent(
    109    browser.selectedBrowser,
    110    "message",
    111    /* capture */ true,
    112    checkFn,
    113    /* wantsUntrusted */ true
    114  ).then(() => {
    115    // An assertion in checkFn wouldn't be recorded as part of the test, so we
    116    // use this assertion to confirm that we've successfully received the
    117    // message (we'll only reach this point if that's the case).
    118    ok(true, "Received message: " + aMessage);
    119  });
    120 }
    121 
    122 function removePermission(url, permission) {
    123  let uri = Cc["@mozilla.org/network/io-service;1"]
    124    .getService(Ci.nsIIOService)
    125    .newURI(url);
    126  let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
    127    Ci.nsIScriptSecurityManager
    128  );
    129  let principal = ssm.createContentPrincipal(uri, {});
    130 
    131  Cc["@mozilla.org/permissionmanager;1"]
    132    .getService(Ci.nsIPermissionManager)
    133    .removeFromPrincipal(principal, permission);
    134 }
    135 
    136 function getPermission(url, permission) {
    137  let uri = Cc["@mozilla.org/network/io-service;1"]
    138    .getService(Ci.nsIIOService)
    139    .newURI(url);
    140  let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
    141    Ci.nsIScriptSecurityManager
    142  );
    143  let principal = ssm.createContentPrincipal(uri, {});
    144 
    145  return Cc["@mozilla.org/permissionmanager;1"]
    146    .getService(Ci.nsIPermissionManager)
    147    .testPermissionFromPrincipal(principal, permission);
    148 }