tor-browser

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

browser_protectionsUI_bounce_tracking_protection.js (3527B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 let btp;
      7 
      8 async function assertProtectionsPanelCounter(value) {
      9  await BrowserTestUtils.waitForCondition(
     10    async () => (await TrackingDBService.sumAllEvents()) == value,
     11    "Waiting for TrackingDBService to record the tracker stats from the purge."
     12  );
     13 
     14  // Open a tab.
     15  let tab = await BrowserTestUtils.openNewForegroundTab(
     16    gBrowser,
     17    "https://example.com"
     18  );
     19  await openProtectionsPanel();
     20 
     21  let trackerCounterBox = document.getElementById(
     22    "protections-popup-trackers-blocked-counter-box"
     23  );
     24  let trackerCounterDesc = document.getElementById(
     25    "protections-popup-trackers-blocked-counter-description"
     26  );
     27 
     28  if (value == 0) {
     29    ok(
     30      BrowserTestUtils.isHidden(trackerCounterBox),
     31      "The blocked tracker counter is hidden if there is no blocked tracker."
     32    );
     33  } else {
     34    ok(
     35      BrowserTestUtils.isVisible(trackerCounterBox),
     36      "The blocked tracker counter is shown if there are blocked trackers."
     37    );
     38    is(
     39      trackerCounterDesc.textContent,
     40      `${value} Blocked`,
     41      "The blocked tracker counter is correct."
     42    );
     43  }
     44 
     45  await closeProtectionsPanel();
     46  BrowserTestUtils.removeTab(tab);
     47 }
     48 
     49 add_setup(async function () {
     50  btp = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
     51    Ci.nsIBounceTrackingProtection
     52  );
     53  // Reset global bounce tracking state.
     54  btp.clearAll();
     55 
     56  // Clear the tracking database.
     57  await TrackingDBService.clearAll();
     58 
     59  registerCleanupFunction(async () => {
     60    // Opening the protections panel for the first time sets this pref. Clean it
     61    // up.
     62    Services.prefs.clearUserPref("browser.protections_panel.infoMessage.seen");
     63  });
     64 });
     65 
     66 add_task(
     67  async function test_purged_bounce_trackers_increment_tracker_counter() {
     68    info("Before purging test that there are no blocked trackers.");
     69    await assertProtectionsPanelCounter(0);
     70 
     71    info("Add bounce trackers to be purged.");
     72    let now = Date.now();
     73    let bounceTrackingGracePeriodSec = Services.prefs.getIntPref(
     74      "privacy.bounceTrackingProtection.bounceTrackingGracePeriodSec"
     75    );
     76    let timestampOutsideGracePeriodThreeDays =
     77      now - (bounceTrackingGracePeriodSec + 60 * 60 * 24 * 3) * 1000;
     78 
     79    const BOUNCE_TRACKERS = ["example.org", "example.com", "example.net"];
     80 
     81    BOUNCE_TRACKERS.forEach(siteHost => {
     82      btp.testAddBounceTrackerCandidate(
     83        {},
     84        siteHost,
     85        timestampOutsideGracePeriodThreeDays * 1000
     86      );
     87    });
     88 
     89    info("Run PurgeBounceTrackers");
     90    await btp.testRunPurgeBounceTrackers();
     91 
     92    // If the mode is enabled, the counter should be incremented.
     93    // If the mode is disabled or dry run, the counter should still be 0.
     94    let expectedCount;
     95    if (
     96      Services.prefs.getIntPref("privacy.bounceTrackingProtection.mode") ==
     97      Ci.nsIBounceTrackingProtection.MODE_ENABLED
     98    ) {
     99      info(
    100        "After purging in enabled state test that the counter is showing and incremented."
    101      );
    102      expectedCount = BOUNCE_TRACKERS.length;
    103    } else {
    104      info(
    105        "After purging in disabled state test that the counter is still 0 and hidden."
    106      );
    107      expectedCount = 0;
    108    }
    109 
    110    await assertProtectionsPanelCounter(expectedCount);
    111    registerCleanupFunction(async () => {
    112      // Reset global bounce tracking state.
    113      btp.clearAll();
    114      // Clear anti tracking db
    115      await TrackingDBService.clearAll();
    116    });
    117  }
    118 );