tor-browser

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

browser_etp_exceptions_dialog.js (3440B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const PERMISSIONS_URL =
      7  "chrome://browser/content/preferences/dialogs/permissions.xhtml";
      8 
      9 const TRACKING_URL = "https://example.com";
     10 
     11 async function openETPExceptionsDialog(doc) {
     12  let exceptionsButton = doc.getElementById("trackingProtectionExceptions");
     13  ok(exceptionsButton, "trackingProtectionExceptions button found");
     14  let dialogPromise = promiseLoadSubDialog(PERMISSIONS_URL);
     15  exceptionsButton.click();
     16  let dialog = await dialogPromise;
     17  return dialog;
     18 }
     19 
     20 async function addETPPermission(doc) {
     21  let dialog = await openETPExceptionsDialog(doc);
     22  let url = dialog.document.getElementById("url");
     23  let buttonDisableETP = dialog.document.getElementById("btnDisableETP");
     24  let permissionsBox = dialog.document.getElementById("permissionsBox");
     25  let currentPermissions = permissionsBox.itemCount;
     26 
     27  url.value = TRACKING_URL;
     28  url.dispatchEvent(new Event("input", { bubbles: true }));
     29  is(
     30    buttonDisableETP.hasAttribute("disabled"),
     31    false,
     32    "Disable ETP button is selectable after url is entered"
     33  );
     34  buttonDisableETP.click();
     35 
     36  // Website is listed
     37  is(
     38    permissionsBox.itemCount,
     39    currentPermissions + 1,
     40    "Website added in url should be in the list"
     41  );
     42  let saveButton = dialog.document.querySelector("dialog").getButton("accept");
     43  saveButton.click();
     44  BrowserTestUtils.waitForEvent(dialog, "unload");
     45 }
     46 
     47 async function removeETPPermission(doc) {
     48  let dialog = await openETPExceptionsDialog(doc);
     49  let permissionsBox = dialog.document.getElementById("permissionsBox");
     50  let elements = permissionsBox.getElementsByAttribute("origin", TRACKING_URL);
     51  // Website is listed
     52  ok(permissionsBox.itemCount, "List is not empty");
     53  permissionsBox.selectItem(elements[0]);
     54  let removePermissionButton =
     55    dialog.document.getElementById("removePermission");
     56  is(
     57    removePermissionButton.hasAttribute("disabled"),
     58    false,
     59    "The button should be clickable to remove selected item"
     60  );
     61  removePermissionButton.click();
     62 
     63  let saveButton = dialog.document.querySelector("dialog").getButton("accept");
     64  saveButton.click();
     65  BrowserTestUtils.waitForEvent(dialog, "unload");
     66 }
     67 
     68 async function checkShieldIcon(shieldIcon) {
     69  // Open the website and check that the tracking protection icon is enabled/disabled
     70  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TRACKING_URL);
     71  let icon = document.getElementById("tracking-protection-icon");
     72  is(
     73    gBrowser.ownerGlobal
     74      .getComputedStyle(icon)
     75      .getPropertyValue("list-style-image"),
     76    shieldIcon,
     77    `The tracking protection icon shows the icon ${shieldIcon}`
     78  );
     79  BrowserTestUtils.removeTab(tab);
     80 }
     81 
     82 // test adds and removes an ETP permission via the about:preferences#privacy and checks if the ProtectionsUI shield icon resembles the state
     83 add_task(async function ETPPermissionSyncedFromPrivacyPane() {
     84  await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
     85    leaveOpen: true,
     86  });
     87  let win = gBrowser.selectedBrowser.contentWindow;
     88  let doc = win.document;
     89  await addETPPermission(doc);
     90  await checkShieldIcon(
     91    `url("chrome://browser/skin/tracking-protection-disabled.svg")`
     92  );
     93  await removeETPPermission(doc);
     94  await checkShieldIcon(`url("chrome://browser/skin/tracking-protection.svg")`);
     95  BrowserTestUtils.removeTab(gBrowser.selectedTab);
     96 });