tor-browser

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

browser_contentblocking_standard_tcp_section.js (4774B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Tests the TCP info box in the ETP standard section of about:preferences#privacy.
      6 */
      7 
      8 ChromeUtils.defineESModuleGetters(this, {
      9  Preferences: "resource://gre/modules/Preferences.sys.mjs",
     10 });
     11 
     12 const COOKIE_BEHAVIOR_PREF = "network.cookie.cookieBehavior";
     13 const CAT_PREF = "browser.contentblocking.category";
     14 
     15 const LEARN_MORE_URL =
     16  Services.urlFormatter.formatURLPref("app.support.baseURL") +
     17  "total-cookie-protection";
     18 
     19 const {
     20  BEHAVIOR_REJECT_TRACKER,
     21  BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
     22 } = Ci.nsICookieService;
     23 
     24 async function testTCPSection({ dFPIEnabled }) {
     25  info(
     26    "Testing TCP preferences section in standard " +
     27      JSON.stringify({ dFPIEnabled })
     28  );
     29 
     30  // In order to test the "standard" category we need to set the default value
     31  // for the cookie behavior pref. A user value would get cleared as soon as we
     32  // switch to "standard".
     33  Services.prefs
     34    .getDefaultBranch("")
     35    .setIntPref(
     36      COOKIE_BEHAVIOR_PREF,
     37      dFPIEnabled
     38        ? BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
     39        : BEHAVIOR_REJECT_TRACKER
     40    );
     41 
     42  // Setting to standard category explicitly, since changing the default cookie
     43  // behavior still switches us to custom initially.
     44  await SpecialPowers.pushPrefEnv({
     45    set: [[CAT_PREF, "standard"]],
     46  });
     47 
     48  const uiEnabled =
     49    Services.prefs.getIntPref(COOKIE_BEHAVIOR_PREF) ==
     50    BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN;
     51 
     52  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     53  let doc = gBrowser.contentDocument;
     54 
     55  let standardRadioOption = doc.getElementById("standardRadio");
     56  let strictRadioOption = doc.getElementById("strictRadio");
     57  let customRadioOption = doc.getElementById("customRadio");
     58 
     59  ok(standardRadioOption.selected, "Standard category is selected");
     60 
     61  let etpStandardTCPBox = doc.getElementById("etpStandardTCPBox");
     62  is(
     63    BrowserTestUtils.isVisible(etpStandardTCPBox),
     64    uiEnabled,
     65    `TCP section in standard is ${uiEnabled ? " " : "not "}visible.`
     66  );
     67 
     68  if (uiEnabled) {
     69    // If visible, test the TCP section elements.
     70    let learnMoreLink = etpStandardTCPBox.querySelector("#tcp-learn-more-link");
     71    ok(learnMoreLink, "Should have a learn more link");
     72    BrowserTestUtils.isVisible(
     73      learnMoreLink,
     74      "Learn more link should be visible."
     75    );
     76    ok(
     77      learnMoreLink.href && !learnMoreLink.href.startsWith("about:blank"),
     78      "Learn more link should be valid."
     79    );
     80    is(
     81      learnMoreLink.href,
     82      LEARN_MORE_URL,
     83      "Learn more link should have the correct target."
     84    );
     85 
     86    let description = etpStandardTCPBox.querySelector("description");
     87    ok(description, "Should have a description element.");
     88    BrowserTestUtils.isVisible(description, "Description should be visible.");
     89 
     90    let title = etpStandardTCPBox.querySelector(
     91      ".content-blocking-warning-title"
     92    );
     93    ok(title, "Should have a title element.");
     94    BrowserTestUtils.isVisible(title, "Title should be visible.");
     95  }
     96 
     97  info("Switch to ETP strict.");
     98  let categoryPrefChange = waitForAndAssertPrefState(CAT_PREF, "strict");
     99  strictRadioOption.click();
    100  await categoryPrefChange;
    101  ok(
    102    !BrowserTestUtils.isVisible(etpStandardTCPBox),
    103    "When strict is selected TCP UI is not visible."
    104  );
    105 
    106  info("Switch to ETP custom.");
    107  categoryPrefChange = waitForAndAssertPrefState(CAT_PREF, "custom");
    108  customRadioOption.click();
    109  await categoryPrefChange;
    110  ok(
    111    !BrowserTestUtils.isVisible(etpStandardTCPBox),
    112    "When custom is selected TCP UI is not visible."
    113  );
    114 
    115  info("Switch back to standard and ensure we show the TCP UI again.");
    116  categoryPrefChange = waitForAndAssertPrefState(CAT_PREF, "standard");
    117  standardRadioOption.click();
    118  await categoryPrefChange;
    119  is(
    120    BrowserTestUtils.isVisible(etpStandardTCPBox),
    121    uiEnabled,
    122    `TCP section in standard is ${uiEnabled ? " " : "not "}visible.`
    123  );
    124 
    125  gBrowser.removeCurrentTab();
    126  await SpecialPowers.popPrefEnv();
    127  Services.prefs.setStringPref(CAT_PREF, "standard");
    128 }
    129 
    130 add_setup(async function () {
    131  // Register cleanup function to restore default cookie behavior.
    132  const defaultPrefs = Services.prefs.getDefaultBranch("");
    133  const previousDefaultCB = defaultPrefs.getIntPref(COOKIE_BEHAVIOR_PREF);
    134 
    135  registerCleanupFunction(function () {
    136    defaultPrefs.setIntPref(COOKIE_BEHAVIOR_PREF, previousDefaultCB);
    137  });
    138 });
    139 
    140 // Clients which don't have dFPI enabled should not see the
    141 // preferences section.
    142 add_task(async function test_dfpi_disabled() {
    143  await testTCPSection({ dFPIEnabled: false });
    144 });
    145 
    146 add_task(async function test_dfpi_enabled() {
    147  await testTCPSection({ dFPIEnabled: true });
    148 });