tor-browser

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

browser_protectionsUI_email_trackers_subview.js (5741B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Bug 1819662 - Testing the tracking category of the protection panel shows the
      8 *               email tracker domain if the email tracking protection is
      9 *               enabled
     10 */
     11 
     12 const { PermissionTestUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/PermissionTestUtils.sys.mjs"
     14 );
     15 
     16 const TEST_PAGE =
     17  "https://www.example.com/browser/browser/base/content/test/protectionsUI/emailTrackingPage.html";
     18 const TEST_TRACKER_PAGE = "https://itisatracker.org/";
     19 
     20 const TP_PREF = "privacy.trackingprotection.enabled";
     21 const EMAIL_TP_PREF = "privacy.trackingprotection.emailtracking.enabled";
     22 
     23 /**
     24 * A helper function to check whether or not an element has "notFound" class.
     25 *
     26 * @param {string} id The id of the testing element.
     27 * @returns {boolean} true when the element has "notFound" class.
     28 */
     29 function notFound(id) {
     30  return document.getElementById(id).classList.contains("notFound");
     31 }
     32 
     33 /**
     34 * A helper function to test the protection UI tracker category.
     35 *
     36 * @param {boolean} blocked - true if the email tracking protection is enabled.
     37 */
     38 async function assertSitesListed(blocked) {
     39  let tab = await BrowserTestUtils.openNewForegroundTab({
     40    url: TEST_PAGE,
     41    gBrowser,
     42  });
     43 
     44  await openProtectionsPanel();
     45 
     46  let categoryItem = document.getElementById(
     47    "protections-popup-category-trackers"
     48  );
     49 
     50  if (!blocked) {
     51    // The tracker category should have the 'notFound' class to indicate that
     52    // no tracker was blocked in the page.
     53    ok(
     54      notFound("protections-popup-category-trackers"),
     55      "Tracker category is not found"
     56    );
     57 
     58    ok(
     59      !BrowserTestUtils.isVisible(categoryItem),
     60      "TP category item is not visible"
     61    );
     62    BrowserTestUtils.removeTab(tab);
     63 
     64    return;
     65  }
     66 
     67  // Testing if the tracker category is visible.
     68 
     69  // Explicitly waiting for the category item becoming visible.
     70  await BrowserTestUtils.waitForMutationCondition(categoryItem, {}, () =>
     71    BrowserTestUtils.isVisible(categoryItem)
     72  );
     73 
     74  ok(BrowserTestUtils.isVisible(categoryItem), "TP category item is visible");
     75 
     76  // Click the tracker category and wait until the tracker view is shown.
     77  let trackersView = document.getElementById("protections-popup-trackersView");
     78  let viewShown = BrowserTestUtils.waitForEvent(trackersView, "ViewShown");
     79  categoryItem.click();
     80  await viewShown;
     81 
     82  ok(true, "Trackers view was shown");
     83 
     84  // Ensure the email tracker is listed on the tracker list.
     85  let listItems = Array.from(
     86    trackersView.querySelectorAll(".protections-popup-list-item")
     87  );
     88  is(listItems.length, 1, "We have 1 trackers in the list");
     89 
     90  let listItem = listItems.find(
     91    item =>
     92      item.querySelector("label").value == "https://email-tracking.example.org"
     93  );
     94  ok(listItem, "Has an item for email-tracking.example.org");
     95  ok(BrowserTestUtils.isVisible(listItem), "List item is visible");
     96 
     97  // Back to the popup main view.
     98  let mainView = document.getElementById("protections-popup-mainView");
     99  viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
    100  let backButton = trackersView.querySelector(".subviewbutton-back");
    101  backButton.click();
    102  await viewShown;
    103 
    104  ok(true, "Main view was shown");
    105 
    106  // Add an iframe to a tracker domain and wait until the content event files.
    107  let contentBlockingEventPromise = waitForContentBlockingEvent(1);
    108  await SpecialPowers.spawn(
    109    tab.linkedBrowser,
    110    [TEST_TRACKER_PAGE],
    111    test_url => {
    112      let ifr = content.document.createElement("iframe");
    113 
    114      content.document.body.appendChild(ifr);
    115      ifr.src = test_url;
    116    }
    117  );
    118  await contentBlockingEventPromise;
    119 
    120  // Click the tracker category again.
    121  viewShown = BrowserTestUtils.waitForEvent(trackersView, "ViewShown");
    122  categoryItem.click();
    123  await viewShown;
    124 
    125  // Ensure both the email tracker and the tracker are listed on the tracker
    126  // list.
    127  listItems = Array.from(
    128    trackersView.querySelectorAll(".protections-popup-list-item")
    129  );
    130  is(listItems.length, 2, "We have 2 trackers in the list");
    131 
    132  listItem = listItems.find(
    133    item =>
    134      item.querySelector("label").value == "https://email-tracking.example.org"
    135  );
    136  ok(listItem, "Has an item for email-tracking.example.org");
    137  ok(BrowserTestUtils.isVisible(listItem), "List item is visible");
    138 
    139  listItem = listItems.find(
    140    item => item.querySelector("label").value == "https://itisatracker.org"
    141  );
    142  ok(listItem, "Has an item for itisatracker.org");
    143  ok(BrowserTestUtils.isVisible(listItem), "List item is visible");
    144 
    145  BrowserTestUtils.removeTab(tab);
    146 }
    147 
    148 add_setup(async function () {
    149  Services.prefs.setBoolPref(TP_PREF, true);
    150 
    151  await UrlClassifierTestUtils.addTestTrackers();
    152 
    153  registerCleanupFunction(() => {
    154    Services.prefs.clearUserPref(TP_PREF);
    155    UrlClassifierTestUtils.cleanupTestTrackers();
    156  });
    157 });
    158 
    159 add_task(async function testTrackersSubView() {
    160  info("Testing trackers subview with TP disabled.");
    161  Services.prefs.setBoolPref(EMAIL_TP_PREF, false);
    162  await assertSitesListed(false);
    163  info("Testing trackers subview with TP enabled.");
    164  Services.prefs.setBoolPref(EMAIL_TP_PREF, true);
    165  await assertSitesListed(true);
    166  info("Testing trackers subview with TP enabled and a CB exception.");
    167  let uri = Services.io.newURI("https://www.example.com");
    168  PermissionTestUtils.add(
    169    uri,
    170    "trackingprotection",
    171    Services.perms.ALLOW_ACTION
    172  );
    173  await assertSitesListed(false);
    174  info("Testing trackers subview with TP enabled and a CB exception removed.");
    175  PermissionTestUtils.remove(uri, "trackingprotection");
    176  await assertSitesListed(true);
    177 
    178  Services.prefs.clearUserPref(EMAIL_TP_PREF);
    179 });