tor-browser

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

browser_etp_status.js (3540B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_setup(async function () {
      7  await SpecialPowers.pushPrefEnv({
      8    set: [
      9      // Show the ETP status section in the privacy pane.
     10      ["browser.settings-redesign.etpStatus.enabled", true],
     11      // Ensure we start from ETP "standard".
     12      ["browser.contentblocking.category", "standard"],
     13    ],
     14  });
     15 });
     16 
     17 /**
     18 * Helper for waiting for and asserting the ETP status item state.
     19 * Can be called both when the item already changed or when we need to wait for the change.
     20 *
     21 * @param {Document} doc - The preferences document.
     22 * @param {*} options - The expected ETP status item state.
     23 * @param {string} options.l10nId - The expected l10nId attribute value.
     24 * @param {string} options.description - The expected description attribute value.
     25 */
     26 async function waitForAndAssertEtpStatusItemState(doc, { l10nId }) {
     27  let etpStatusItem = doc.getElementById("etpStatusItem");
     28  Assert.ok(
     29    BrowserTestUtils.isVisible(etpStatusItem),
     30    "ETP status box item should be visible"
     31  );
     32 
     33  let condition = () => {
     34    return etpStatusItem.getAttribute("data-l10n-id") == l10nId;
     35  };
     36  if (!condition()) {
     37    await BrowserTestUtils.waitForMutationCondition(
     38      etpStatusItem,
     39      { attributes: true, attributeFilter: ["data-l10n-id", "description"] },
     40      condition
     41    );
     42  }
     43 
     44  Assert.equal(etpStatusItem.getAttribute("data-l10n-id"), l10nId);
     45 }
     46 
     47 // Test that the ETP status section updates correctly when changing the ETP category.
     48 add_task(async function test_status_categories() {
     49  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     50  let doc = gBrowser.contentDocument;
     51 
     52  await waitForAndAssertEtpStatusItemState(doc, {
     53    l10nId: "preferences-etp-level-standard",
     54  });
     55 
     56  info("Switch to ETP `strict`");
     57  Services.prefs.setStringPref("browser.contentblocking.category", "strict");
     58 
     59  await waitForAndAssertEtpStatusItemState(doc, {
     60    l10nId: "preferences-etp-level-strict",
     61  });
     62 
     63  info("Switch to ETP `custom`");
     64  Services.prefs.setStringPref("browser.contentblocking.category", "custom");
     65 
     66  await waitForAndAssertEtpStatusItemState(doc, {
     67    l10nId: "preferences-etp-level-custom",
     68  });
     69 
     70  info("Switch back to default ETP `standard`");
     71  Services.prefs.clearUserPref("browser.contentblocking.category");
     72 
     73  await waitForAndAssertEtpStatusItemState(doc, {
     74    l10nId: "preferences-etp-level-standard",
     75  });
     76 
     77  gBrowser.removeCurrentTab();
     78 });
     79 
     80 // Test that the protections dashboard link in the ETP status section opens the about:protections page.
     81 add_task(async function test_protections_dashboard_link() {
     82  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     83  let doc = gBrowser.contentDocument;
     84 
     85  let protectionsDashboardLink = doc.getElementById("protectionsDashboardLink");
     86 
     87  Assert.ok(
     88    BrowserTestUtils.isVisible(protectionsDashboardLink),
     89    "Protections dashboard link is visible."
     90  );
     91 
     92  let linkPromise = BrowserTestUtils.waitForNewTab(
     93    gBrowser,
     94    "about:protections"
     95  );
     96  await BrowserTestUtils.synthesizeMouseAtCenter(
     97    "#protectionsDashboardLink",
     98    {},
     99    gBrowser.selectedBrowser
    100  );
    101  let tab = await linkPromise;
    102 
    103  Assert.equal(
    104    tab.linkedBrowser.currentURI.spec,
    105    "about:protections",
    106    "Protections dashboard link opened the about:protections page."
    107  );
    108 
    109  // about:protections tab.
    110  BrowserTestUtils.removeTab(tab);
    111  // Preferences tab.
    112  gBrowser.removeCurrentTab();
    113 });