tor-browser

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

browser_permissions.js (19672B)


      1 /*
      2 * Test the Permissions section in the Control Center.
      3 */
      4 
      5 const PERMISSIONS_PAGE =
      6  getRootDirectory(gTestPath).replace(
      7    "chrome://mochitests/content",
      8    "https://example.com"
      9  ) + "permissions.html";
     10 
     11 function testPermListHasEntries(expectEntries) {
     12  let permissionsList = document.getElementById(
     13    "permission-popup-permission-list"
     14  );
     15  let listEntryCount = permissionsList.querySelectorAll(
     16    ".permission-popup-permission-item"
     17  ).length;
     18  if (expectEntries) {
     19    ok(listEntryCount, "List of permissions is not empty");
     20    return;
     21  }
     22  ok(!listEntryCount, "List of permissions is empty");
     23 }
     24 
     25 add_task(async function testMainViewVisible() {
     26  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function () {
     27    await openPermissionPopup();
     28 
     29    let permissionsList = document.getElementById(
     30      "permission-popup-permission-list"
     31    );
     32    testPermListHasEntries(false);
     33 
     34    await closePermissionPopup();
     35 
     36    PermissionTestUtils.add(
     37      gBrowser.currentURI,
     38      "camera",
     39      Services.perms.ALLOW_ACTION
     40    );
     41 
     42    await openPermissionPopup();
     43 
     44    testPermListHasEntries(true);
     45 
     46    let labelText = SitePermissions.getPermissionLabel("camera");
     47    let labels = permissionsList.querySelectorAll(
     48      ".permission-popup-permission-label"
     49    );
     50    is(labels.length, 1, "One permission visible in main view");
     51    is(labels[0].innerHTML, labelText, "Correct value");
     52 
     53    let img = permissionsList.querySelector(
     54      "image.permission-popup-permission-icon"
     55    );
     56    ok(img, "There is an image for the permissions");
     57    ok(img.classList.contains("camera-icon"), "proper class is in image class");
     58 
     59    await closePermissionPopup();
     60 
     61    PermissionTestUtils.remove(gBrowser.currentURI, "camera");
     62 
     63    // We intentionally turn off a11y_checks, because the following function
     64    // is expected to click a toolbar button that may be already hidden
     65    // with "display:none;". The permissions panel anchor is hidden because
     66    // the last permission was removed, however we force opening the panel
     67    // anyways in order to test that the list has been properly emptied:
     68    AccessibilityUtils.setEnv({
     69      mustHaveAccessibleRule: false,
     70    });
     71    await openPermissionPopup();
     72    AccessibilityUtils.resetEnv();
     73 
     74    testPermListHasEntries(false);
     75 
     76    await closePermissionPopup();
     77  });
     78 });
     79 
     80 add_task(async function testIdentityIcon() {
     81  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, function () {
     82    PermissionTestUtils.add(
     83      gBrowser.currentURI,
     84      "geo",
     85      Services.perms.ALLOW_ACTION
     86    );
     87 
     88    ok(
     89      gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
     90      "identity-box signals granted permissions"
     91    );
     92 
     93    PermissionTestUtils.remove(gBrowser.currentURI, "geo");
     94 
     95    ok(
     96      !gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
     97      "identity-box doesn't signal granted permissions"
     98    );
     99 
    100    PermissionTestUtils.add(
    101      gBrowser.currentURI,
    102      "not-a-site-permission",
    103      Services.perms.ALLOW_ACTION
    104    );
    105 
    106    ok(
    107      !gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
    108      "identity-box doesn't signal granted permissions"
    109    );
    110 
    111    PermissionTestUtils.add(
    112      gBrowser.currentURI,
    113      "cookie",
    114      Ci.nsICookiePermission.ACCESS_SESSION
    115    );
    116 
    117    ok(
    118      gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
    119      "identity-box signals granted permissions"
    120    );
    121 
    122    PermissionTestUtils.remove(gBrowser.currentURI, "cookie");
    123 
    124    PermissionTestUtils.add(
    125      gBrowser.currentURI,
    126      "cookie",
    127      Ci.nsICookiePermission.ACCESS_DENY
    128    );
    129 
    130    ok(
    131      gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
    132      "identity-box signals granted permissions"
    133    );
    134 
    135    PermissionTestUtils.add(
    136      gBrowser.currentURI,
    137      "cookie",
    138      Ci.nsICookiePermission.ACCESS_DEFAULT
    139    );
    140 
    141    ok(
    142      !gPermissionPanel._identityPermissionBox.hasAttribute("hasPermissions"),
    143      "identity-box doesn't signal granted permissions"
    144    );
    145 
    146    PermissionTestUtils.remove(gBrowser.currentURI, "geo");
    147    PermissionTestUtils.remove(gBrowser.currentURI, "not-a-site-permission");
    148    PermissionTestUtils.remove(gBrowser.currentURI, "cookie");
    149  });
    150 });
    151 
    152 add_task(async function testCancelPermission() {
    153  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function () {
    154    let permissionsList = document.getElementById(
    155      "permission-popup-permission-list"
    156    );
    157 
    158    PermissionTestUtils.add(
    159      gBrowser.currentURI,
    160      "geo",
    161      Services.perms.ALLOW_ACTION
    162    );
    163    PermissionTestUtils.add(
    164      gBrowser.currentURI,
    165      "camera",
    166      Services.perms.DENY_ACTION
    167    );
    168 
    169    await openPermissionPopup();
    170 
    171    testPermListHasEntries(true);
    172 
    173    permissionsList
    174      .querySelector(".permission-popup-permission-remove-button")
    175      .click();
    176 
    177    is(
    178      permissionsList.querySelectorAll(".permission-popup-permission-label")
    179        .length,
    180      1,
    181      "First permission should be removed"
    182    );
    183 
    184    permissionsList
    185      .querySelector(".permission-popup-permission-remove-button")
    186      .click();
    187 
    188    is(
    189      permissionsList.querySelectorAll(".permission-popup-permission-label")
    190        .length,
    191      0,
    192      "Second permission should be removed"
    193    );
    194 
    195    await closePermissionPopup();
    196  });
    197 });
    198 
    199 add_task(async function testPermissionHints() {
    200  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
    201    let permissionsList = document.getElementById(
    202      "permission-popup-permission-list"
    203    );
    204    let reloadHint = document.getElementById(
    205      "permission-popup-permission-reload-hint"
    206    );
    207 
    208    await openPermissionPopup();
    209 
    210    ok(BrowserTestUtils.isHidden(reloadHint), "Reload hint is hidden");
    211 
    212    await closePermissionPopup();
    213 
    214    PermissionTestUtils.add(
    215      gBrowser.currentURI,
    216      "geo",
    217      Services.perms.ALLOW_ACTION
    218    );
    219    PermissionTestUtils.add(
    220      gBrowser.currentURI,
    221      "camera",
    222      Services.perms.DENY_ACTION
    223    );
    224 
    225    await openPermissionPopup();
    226 
    227    ok(BrowserTestUtils.isHidden(reloadHint), "Reload hint is hidden");
    228 
    229    let cancelButtons = permissionsList.querySelectorAll(
    230      ".permission-popup-permission-remove-button"
    231    );
    232    PermissionTestUtils.remove(gBrowser.currentURI, "camera");
    233 
    234    cancelButtons[0].click();
    235    ok(!BrowserTestUtils.isHidden(reloadHint), "Reload hint is visible");
    236 
    237    cancelButtons[1].click();
    238    ok(!BrowserTestUtils.isHidden(reloadHint), "Reload hint is visible");
    239 
    240    await closePermissionPopup();
    241    let loaded = BrowserTestUtils.browserLoaded(browser);
    242    BrowserTestUtils.startLoadingURIString(browser, PERMISSIONS_PAGE);
    243    await loaded;
    244    await openPermissionPopup();
    245 
    246    ok(
    247      BrowserTestUtils.isHidden(reloadHint),
    248      "Reload hint is hidden after reloading"
    249    );
    250 
    251    await closePermissionPopup();
    252  });
    253 });
    254 
    255 add_task(async function testPermissionIcons() {
    256  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, function () {
    257    PermissionTestUtils.add(
    258      gBrowser.currentURI,
    259      "camera",
    260      Services.perms.ALLOW_ACTION
    261    );
    262    PermissionTestUtils.add(
    263      gBrowser.currentURI,
    264      "geo",
    265      Services.perms.DENY_ACTION
    266    );
    267 
    268    let geoIcon = gPermissionPanel._identityPermissionBox.querySelector(
    269      ".blocked-permission-icon[data-permission-id='geo']"
    270    );
    271    ok(geoIcon.hasAttribute("showing"), "blocked permission icon is shown");
    272 
    273    let cameraIcon = gPermissionPanel._identityPermissionBox.querySelector(
    274      ".blocked-permission-icon[data-permission-id='camera']"
    275    );
    276    ok(
    277      !cameraIcon.hasAttribute("showing"),
    278      "allowed permission icon is not shown"
    279    );
    280 
    281    PermissionTestUtils.remove(gBrowser.currentURI, "geo");
    282 
    283    ok(
    284      !geoIcon.hasAttribute("showing"),
    285      "blocked permission icon is not shown after reset"
    286    );
    287 
    288    PermissionTestUtils.remove(gBrowser.currentURI, "camera");
    289  });
    290 });
    291 
    292 add_task(async function testPermissionShortcuts() {
    293  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
    294    browser.focus();
    295 
    296    await new Promise(r => {
    297      SpecialPowers.pushPrefEnv(
    298        { set: [["permissions.default.shortcuts", 0]] },
    299        r
    300      );
    301    });
    302 
    303    async function tryKey(desc, expectedValue) {
    304      await EventUtils.synthesizeAndWaitKey("c", { accelKey: true });
    305      let result = await SpecialPowers.spawn(browser, [], function () {
    306        return {
    307          keydowns: content.wrappedJSObject.gKeyDowns,
    308          keypresses: content.wrappedJSObject.gKeyPresses,
    309        };
    310      });
    311      is(
    312        result.keydowns,
    313        expectedValue,
    314        "keydown event was fired or not fired as expected, " + desc
    315      );
    316      is(
    317        result.keypresses,
    318        0,
    319        "keypress event shouldn't be fired for shortcut key, " + desc
    320      );
    321    }
    322 
    323    await tryKey("pressed with default permissions", 1);
    324 
    325    PermissionTestUtils.add(
    326      gBrowser.currentURI,
    327      "shortcuts",
    328      Services.perms.DENY_ACTION
    329    );
    330    await tryKey("pressed when site blocked", 1);
    331 
    332    PermissionTestUtils.add(
    333      gBrowser.currentURI,
    334      "shortcuts",
    335      PermissionTestUtils.ALLOW
    336    );
    337    await tryKey("pressed when site allowed", 2);
    338 
    339    PermissionTestUtils.remove(gBrowser.currentURI, "shortcuts");
    340    await new Promise(r => {
    341      SpecialPowers.pushPrefEnv(
    342        { set: [["permissions.default.shortcuts", 2]] },
    343        r
    344      );
    345    });
    346 
    347    await tryKey("pressed when globally blocked", 2);
    348    PermissionTestUtils.add(
    349      gBrowser.currentURI,
    350      "shortcuts",
    351      Services.perms.ALLOW_ACTION
    352    );
    353    await tryKey("pressed when globally blocked but site allowed", 3);
    354 
    355    PermissionTestUtils.add(
    356      gBrowser.currentURI,
    357      "shortcuts",
    358      Services.perms.DENY_ACTION
    359    );
    360    await tryKey("pressed when globally blocked and site blocked", 3);
    361 
    362    PermissionTestUtils.remove(gBrowser.currentURI, "shortcuts");
    363  });
    364 });
    365 
    366 // Test the control center UI when policy permissions are set.
    367 add_task(async function testPolicyPermission() {
    368  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function () {
    369    await SpecialPowers.pushPrefEnv({
    370      set: [["dom.disable_open_during_load", true]],
    371    });
    372 
    373    let permissionsList = document.getElementById(
    374      "permission-popup-permission-list"
    375    );
    376    PermissionTestUtils.add(
    377      gBrowser.currentURI,
    378      "popup",
    379      Services.perms.ALLOW_ACTION,
    380      Services.perms.EXPIRE_POLICY
    381    );
    382 
    383    await openPermissionPopup();
    384 
    385    // Check if the icon, nameLabel and stateLabel are visible.
    386    let img, labelText, labels;
    387 
    388    img = permissionsList.querySelector(
    389      "image.permission-popup-permission-icon"
    390    );
    391    ok(img, "There is an image for the popup permission");
    392    ok(img.classList.contains("popup-icon"), "proper class is in image class");
    393 
    394    labelText = SitePermissions.getPermissionLabel("popup");
    395    labels = permissionsList.querySelectorAll(
    396      ".permission-popup-permission-label"
    397    );
    398    is(labels.length, 1, "One permission visible in main view");
    399    is(labels[0].innerHTML, labelText, "Correct name label value");
    400 
    401    labelText = SitePermissions.getCurrentStateLabel(
    402      SitePermissions.ALLOW,
    403      SitePermissions.SCOPE_POLICY
    404    );
    405    labels = permissionsList.querySelectorAll(
    406      ".permission-popup-permission-state-label"
    407    );
    408    is(labels[0].innerHTML, labelText, "Correct state label value");
    409 
    410    // Check if the menulist and the remove button are hidden.
    411    // The menulist is specific to the "popup" permission.
    412    let menulist = document.getElementById("permission-popup-menulist");
    413    Assert.equal(
    414      menulist,
    415      null,
    416      "The popup permission menulist is not visible"
    417    );
    418 
    419    let removeButton = permissionsList.querySelector(
    420      ".permission-popup-permission-remove-button"
    421    );
    422    Assert.equal(
    423      removeButton,
    424      null,
    425      "The permission remove button is not visible"
    426    );
    427 
    428    Services.perms.removeAll();
    429    await closePermissionPopup();
    430  });
    431 });
    432 
    433 add_task(async function testHiddenAfterRefresh() {
    434  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
    435    ok(
    436      BrowserTestUtils.isHidden(gPermissionPanel._permissionPopup),
    437      "Popup is hidden"
    438    );
    439 
    440    await openPermissionPopup();
    441 
    442    ok(
    443      !BrowserTestUtils.isHidden(gPermissionPanel._permissionPopup),
    444      "Popup is shown"
    445    );
    446 
    447    let reloaded = BrowserTestUtils.browserLoaded(
    448      browser,
    449      false,
    450      PERMISSIONS_PAGE
    451    );
    452    EventUtils.synthesizeKey("VK_F5", {}, browser.ownerGlobal);
    453    await reloaded;
    454 
    455    ok(
    456      BrowserTestUtils.isHidden(gPermissionPanel._permissionPopup),
    457      "Popup is hidden"
    458    );
    459  });
    460 });
    461 
    462 async function helper3rdPartyStoragePermissionTest(permissionID) {
    463  // 3rdPartyStorage permissions are listed under an anchor container - test
    464  // that this works correctly, i.e. the permission items are added to the
    465  // anchor when relevant, and other permission items are added to the default
    466  // anchor, and adding/removing permissions preserves this behavior correctly.
    467 
    468  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
    469    await openPermissionPopup();
    470 
    471    let permissionsList = document.getElementById(
    472      "permission-popup-permission-list"
    473    );
    474    let storagePermissionAnchor = permissionsList.querySelector(
    475      `.permission-popup-permission-list-anchor[anchorfor="3rdPartyStorage"]`
    476    );
    477 
    478    testPermListHasEntries(false);
    479 
    480    ok(
    481      BrowserTestUtils.isHidden(storagePermissionAnchor.firstElementChild),
    482      "Anchor header is hidden"
    483    );
    484 
    485    await closePermissionPopup();
    486 
    487    let storagePermissionID = `${permissionID}^https://example2.com`;
    488    PermissionTestUtils.add(
    489      browser.currentURI,
    490      storagePermissionID,
    491      Services.perms.ALLOW_ACTION
    492    );
    493 
    494    await openPermissionPopup();
    495 
    496    testPermListHasEntries(true);
    497    ok(
    498      BrowserTestUtils.isVisible(storagePermissionAnchor.firstElementChild),
    499      "Anchor header is visible"
    500    );
    501 
    502    let labelText = SitePermissions.getPermissionLabel(storagePermissionID);
    503    let labels = storagePermissionAnchor.querySelectorAll(
    504      ".permission-popup-permission-label"
    505    );
    506    is(labels.length, 1, "One permission visible in 3rdPartyStorage anchor");
    507    is(
    508      labels[0].getAttribute("value"),
    509      labelText,
    510      "Permission label has the correct value"
    511    );
    512 
    513    await closePermissionPopup();
    514 
    515    PermissionTestUtils.add(
    516      browser.currentURI,
    517      "camera",
    518      Services.perms.ALLOW_ACTION
    519    );
    520 
    521    await openPermissionPopup();
    522 
    523    testPermListHasEntries(true);
    524    ok(
    525      BrowserTestUtils.isVisible(storagePermissionAnchor.firstElementChild),
    526      "Anchor header is visible"
    527    );
    528 
    529    labels = permissionsList.querySelectorAll(
    530      ".permission-popup-permission-label"
    531    );
    532    is(labels.length, 2, "Two permissions visible in main view");
    533    labels = storagePermissionAnchor.querySelectorAll(
    534      ".permission-popup-permission-label"
    535    );
    536    is(labels.length, 1, "One permission visible in 3rdPartyStorage anchor");
    537 
    538    storagePermissionAnchor
    539      .querySelector(".permission-popup-permission-remove-button")
    540      .click();
    541    is(
    542      storagePermissionAnchor.querySelectorAll(
    543        ".permission-popup-permission-label"
    544      ).length,
    545      0,
    546      "Permission item should be removed"
    547    );
    548    is(
    549      PermissionTestUtils.testPermission(
    550        browser.currentURI,
    551        storagePermissionID
    552      ),
    553      SitePermissions.UNKNOWN,
    554      "Permission removed from permission manager"
    555    );
    556 
    557    await closePermissionPopup();
    558 
    559    await openPermissionPopup();
    560 
    561    testPermListHasEntries(true);
    562    ok(
    563      BrowserTestUtils.isHidden(storagePermissionAnchor.firstElementChild),
    564      "Anchor header is hidden"
    565    );
    566 
    567    labels = permissionsList.querySelectorAll(
    568      ".permission-popup-permission-label"
    569    );
    570    is(labels.length, 1, "One permission visible in main view");
    571 
    572    await closePermissionPopup();
    573 
    574    PermissionTestUtils.remove(browser.currentURI, "camera");
    575 
    576    await openPermissionPopup();
    577 
    578    testPermListHasEntries(false);
    579    ok(
    580      BrowserTestUtils.isHidden(storagePermissionAnchor.firstElementChild),
    581      "Anchor header is hidden"
    582    );
    583 
    584    await closePermissionPopup();
    585  });
    586 }
    587 
    588 add_task(async function test3rdPartyStoragePermission() {
    589  await helper3rdPartyStoragePermissionTest("3rdPartyStorage");
    590 });
    591 
    592 add_task(async function test3rdPartyFrameStoragePermission() {
    593  await helper3rdPartyStoragePermissionTest("3rdPartyFrameStorage");
    594 });
    595 
    596 add_task(async function test3rdPartyBothStoragePermission() {
    597  // Test the handling of both types of 3rdParty(Frame)?Storage permissions together
    598 
    599  await BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function (browser) {
    600    await openPermissionPopup();
    601 
    602    let permissionsList = document.getElementById(
    603      "permission-popup-permission-list"
    604    );
    605    let storagePermissionAnchor = permissionsList.querySelector(
    606      `.permission-popup-permission-list-anchor[anchorfor="3rdPartyStorage"]`
    607    );
    608 
    609    testPermListHasEntries(false);
    610 
    611    ok(
    612      BrowserTestUtils.isHidden(storagePermissionAnchor.firstElementChild),
    613      "Anchor header is hidden"
    614    );
    615 
    616    await closePermissionPopup();
    617 
    618    let storagePermissionID = "3rdPartyFrameStorage^https://example2.com";
    619    PermissionTestUtils.add(
    620      browser.currentURI,
    621      storagePermissionID,
    622      Services.perms.ALLOW_ACTION
    623    );
    624 
    625    await openPermissionPopup();
    626 
    627    testPermListHasEntries(true);
    628    ok(
    629      BrowserTestUtils.isVisible(storagePermissionAnchor.firstElementChild),
    630      "Anchor header is visible"
    631    );
    632 
    633    let labelText = SitePermissions.getPermissionLabel(storagePermissionID);
    634    let labels = storagePermissionAnchor.querySelectorAll(
    635      ".permission-popup-permission-label"
    636    );
    637    is(labels.length, 1, "One permission visible in 3rdPartyStorage anchor");
    638    is(
    639      labels[0].getAttribute("value"),
    640      labelText,
    641      "Permission label has the correct value"
    642    );
    643 
    644    await closePermissionPopup();
    645 
    646    PermissionTestUtils.add(
    647      browser.currentURI,
    648      "3rdPartyStorage^https://www.example2.com",
    649      Services.perms.ALLOW_ACTION
    650    );
    651 
    652    await openPermissionPopup();
    653 
    654    testPermListHasEntries(true);
    655    ok(
    656      BrowserTestUtils.isVisible(storagePermissionAnchor.firstElementChild),
    657      "Anchor header is visible"
    658    );
    659 
    660    labels = permissionsList.querySelectorAll(
    661      ".permission-popup-permission-label"
    662    );
    663    is(labels.length, 1, "One permissions visible in main view");
    664    labels = storagePermissionAnchor.querySelectorAll(
    665      ".permission-popup-permission-label"
    666    );
    667    is(labels.length, 1, "One permission visible in 3rdPartyStorage anchor");
    668 
    669    storagePermissionAnchor
    670      .querySelector(".permission-popup-permission-remove-button")
    671      .click();
    672    is(
    673      storagePermissionAnchor.querySelectorAll(
    674        ".permission-popup-permission-label"
    675      ).length,
    676      0,
    677      "Permission item should be removed"
    678    );
    679    is(
    680      PermissionTestUtils.testPermission(
    681        browser.currentURI,
    682        storagePermissionID
    683      ),
    684      SitePermissions.UNKNOWN,
    685      "Permission removed from permission manager"
    686    );
    687    is(
    688      PermissionTestUtils.testPermission(
    689        browser.currentURI,
    690        "3rdPartyStorage^https://www.example2.com"
    691      ),
    692      SitePermissions.UNKNOWN,
    693      "3rdPartyStorage permission removed from permission manager"
    694    );
    695 
    696    await closePermissionPopup();
    697  });
    698 });