tor-browser

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

browser_permissions_dismiss.js (5762B)


      1 "use strict";
      2 
      3 const INSTALL_PAGE = `${BASE}/file_install_extensions.html`;
      4 const INSTALL_XPI = `${BASE}/browser_webext_permissions.xpi`;
      5 const ID = "permissions@test.mozilla.org"; // Add-on ID of INSTALL_XPI.
      6 
      7 // With the new dialog design both wildcards and non-wildcards host
      8 // permissions are expected to be shown as a single permission entry
      9 const expectedPermsCount = 4;
     10 
     11 function assertPermissionsListCount({ grantedPermissionsCount }) {
     12  let permsUL = document.getElementById("addon-webext-perm-list-required");
     13  is(
     14    permsUL.childElementCount,
     15    grantedPermissionsCount,
     16    `Permissions list should have ${grantedPermissionsCount} entries`
     17  );
     18 }
     19 
     20 add_setup(async function () {
     21  await SpecialPowers.pushPrefEnv({
     22    set: [
     23      ["extensions.webapi.testing", true],
     24      ["extensions.install.requireBuiltInCerts", false],
     25    ],
     26  });
     27 
     28  registerCleanupFunction(async () => {
     29    await SpecialPowers.popPrefEnv();
     30  });
     31 });
     32 
     33 add_task(async function test_tab_switch_dismiss() {
     34  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, INSTALL_PAGE);
     35 
     36  let installCanceled = new Promise(resolve => {
     37    let listener = {
     38      onInstallCancelled() {
     39        AddonManager.removeInstallListener(listener);
     40        resolve();
     41      },
     42    };
     43    AddonManager.addInstallListener(listener);
     44  });
     45 
     46  SpecialPowers.spawn(gBrowser.selectedBrowser, [INSTALL_XPI], function (url) {
     47    content.wrappedJSObject.installMozAM(url);
     48  });
     49 
     50  const panel = await promisePopupNotificationShown("addon-webext-permissions");
     51  assertPermissionsListCount({ grantedPermissionsCount: expectedPermsCount });
     52 
     53  let permsLearnMore = panel.querySelector(
     54    ".popup-notification-learnmore-link"
     55  );
     56  is(
     57    permsLearnMore.href,
     58    Services.urlFormatter.formatURLPref("app.support.baseURL") +
     59      "extension-permissions",
     60    "Learn more link has desired URL"
     61  );
     62  ok(
     63    BrowserTestUtils.isVisible(permsLearnMore),
     64    "Learn more link is shown on Permission popup"
     65  );
     66 
     67  // Switching tabs dismisses the notification and cancels the install.
     68  let switchTo = await BrowserTestUtils.openNewForegroundTab(gBrowser);
     69  BrowserTestUtils.removeTab(switchTo);
     70  await installCanceled;
     71 
     72  let addon = await AddonManager.getAddonByID(ID);
     73  is(addon, null, "Extension is not installed");
     74 
     75  BrowserTestUtils.removeTab(tab);
     76 });
     77 
     78 add_task(async function test_add_tab_by_user_and_switch() {
     79  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, INSTALL_PAGE);
     80 
     81  let listener = {
     82    onInstallCancelled() {
     83      this.canceledPromise = Promise.resolve();
     84    },
     85  };
     86  AddonManager.addInstallListener(listener);
     87 
     88  SpecialPowers.spawn(gBrowser.selectedBrowser, [INSTALL_XPI], function (url) {
     89    content.wrappedJSObject.installMozAM(url);
     90  });
     91 
     92  // Show addon permission notification.
     93  await promisePopupNotificationShown("addon-webext-permissions");
     94 
     95  assertPermissionsListCount({ grantedPermissionsCount: expectedPermsCount });
     96 
     97  info("Verify permissions list again after switching active tab");
     98 
     99  // Open about:newtab page in a new tab.
    100  let newTab = await BrowserTestUtils.openNewForegroundTab(
    101    gBrowser,
    102    "about:newtab",
    103    false
    104  );
    105 
    106  // Switch to tab that is opening addon permission notification.
    107  gBrowser.selectedTab = tab;
    108 
    109  assertPermissionsListCount({ grantedPermissionsCount: expectedPermsCount });
    110 
    111  ok(!listener.canceledPromise, "Extension installation is not canceled");
    112 
    113  // Cancel installation.
    114  document.querySelector(".popup-notification-secondary-button").click();
    115  await listener.canceledPromise;
    116  info("Extension installation is canceled");
    117 
    118  let addon = await AddonManager.getAddonByID(ID);
    119  is(addon, null, "Extension is not installed");
    120 
    121  AddonManager.removeInstallListener(listener);
    122  BrowserTestUtils.removeTab(tab);
    123  BrowserTestUtils.removeTab(newTab);
    124 });
    125 
    126 // Regression test for https://bugzilla.mozilla.org/show_bug.cgi?id=1974419
    127 // ExtensionPermissions.get() lazily populates the StartupCache. This method
    128 // should not be used when an extension is not installed, to avoid persisting
    129 // permission data for a non-installed extension.
    130 add_task(async function test_no_permissions_stored_after_dismiss() {
    131  // This part could fail if any of the tests before (which also trigger
    132  // installation of INSTALL_XPI) somehow populate the permissions.
    133  Assert.deepEqual(
    134    await getCachedPermissions(ID),
    135    null,
    136    "ExtensionPermissions should not contain entry before installation"
    137  );
    138 
    139  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, INSTALL_PAGE);
    140 
    141  let installCanceled = new Promise(resolve => {
    142    let listener = {
    143      onInstallCancelled() {
    144        AddonManager.removeInstallListener(listener);
    145        resolve();
    146      },
    147    };
    148    AddonManager.addInstallListener(listener);
    149  });
    150 
    151  SpecialPowers.spawn(gBrowser.selectedBrowser, [INSTALL_XPI], function (url) {
    152    content.wrappedJSObject.installMozAM(url);
    153  });
    154 
    155  const panel = await promisePopupNotificationShown("addon-webext-permissions");
    156 
    157  let privateBrowsingCheckbox = panel.querySelector(
    158    ".webext-perm-privatebrowsing > moz-checkbox"
    159  );
    160  ok(
    161    BrowserTestUtils.isVisible(privateBrowsingCheckbox),
    162    "Private browsing checkbox is present in install prompt"
    163  );
    164  Assert.deepEqual(
    165    await getCachedPermissions(ID),
    166    null,
    167    "ExtensionPermissions should not be written to during installation"
    168  );
    169 
    170  // Cancel installation.
    171  panel.secondaryButton.click();
    172  await installCanceled;
    173 
    174  let addon = await AddonManager.getAddonByID(ID);
    175  is(addon, null, "Extension is not installed");
    176 
    177  Assert.deepEqual(
    178    await getCachedPermissions(ID),
    179    null,
    180    "ExtensionPermissions should not be written to after installation"
    181  );
    182 
    183  BrowserTestUtils.removeTab(tab);
    184 });