tor-browser

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

browser_protectionsUI_socialtracking.js (9250B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TRACKING_PAGE =
      7  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      8  "http://example.com/browser/browser/base/content/test/protectionsUI/trackingPage.html";
      9 
     10 const ST_PROTECTION_PREF = "privacy.trackingprotection.socialtracking.enabled";
     11 const ST_BLOCK_COOKIES_PREF = "privacy.socialtracking.block_cookies.enabled";
     12 
     13 add_setup(async function () {
     14  await SpecialPowers.pushPrefEnv({
     15    set: [
     16      [ST_BLOCK_COOKIES_PREF, true],
     17      [
     18        "urlclassifier.features.socialtracking.blacklistHosts",
     19        "social-tracking.example.org",
     20      ],
     21      [
     22        "urlclassifier.features.socialtracking.annotate.blacklistHosts",
     23        "social-tracking.example.org",
     24      ],
     25      // Whitelist trackertest.org loaded by default in trackingPage.html
     26      ["urlclassifier.trackingSkipURLs", "*://trackertest.org/*"],
     27      ["urlclassifier.trackingAnnotationSkipURLs", "*://trackertest.org/*"],
     28      ["privacy.trackingprotection.enabled", false],
     29      ["privacy.trackingprotection.annotate_channels", true],
     30    ],
     31  });
     32 });
     33 
     34 async function testIdentityState(hasException) {
     35  let promise = BrowserTestUtils.openNewForegroundTab({
     36    url: TRACKING_PAGE,
     37    gBrowser,
     38  });
     39  let [tab] = await Promise.all([promise, waitForContentBlockingEvent()]);
     40 
     41  if (hasException) {
     42    let loaded = BrowserTestUtils.browserLoaded(
     43      tab.linkedBrowser,
     44      false,
     45      TRACKING_PAGE
     46    );
     47    gProtectionsHandler.disableForCurrentPage();
     48    await loaded;
     49  }
     50 
     51  await openProtectionsPanel();
     52  let categoryItem = document.getElementById(
     53    "protections-popup-category-socialblock"
     54  );
     55 
     56  ok(
     57    categoryItem.classList.contains("notFound"),
     58    "socialtrackings are not detected"
     59  );
     60 
     61  ok(
     62    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
     63    "icon box is visible regardless the exception"
     64  );
     65  await closeProtectionsPanel();
     66 
     67  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
     68    content.postMessage("socialtracking", "*");
     69  });
     70  await openProtectionsPanel();
     71 
     72  await TestUtils.waitForCondition(() => {
     73    return !categoryItem.classList.contains("notFound");
     74  });
     75 
     76  ok(
     77    gProtectionsHandler._protectionsPopup.hasAttribute("detected"),
     78    "trackers are detected"
     79  );
     80  ok(
     81    !categoryItem.classList.contains("notFound"),
     82    "social trackers are detected"
     83  );
     84  ok(
     85    BrowserTestUtils.isVisible(gProtectionsHandler.iconBox),
     86    "icon box is visible"
     87  );
     88  is(
     89    gProtectionsHandler.iconBox.hasAttribute("hasException"),
     90    hasException,
     91    "Shows an exception when appropriate"
     92  );
     93  await closeProtectionsPanel();
     94 
     95  if (hasException) {
     96    let loaded = BrowserTestUtils.browserLoaded(
     97      tab.linkedBrowser,
     98      false,
     99      TRACKING_PAGE
    100    );
    101    gProtectionsHandler.enableForCurrentPage();
    102    await loaded;
    103  }
    104 
    105  BrowserTestUtils.removeTab(tab);
    106 }
    107 
    108 async function testSubview(hasException) {
    109  let promise = BrowserTestUtils.openNewForegroundTab({
    110    url: TRACKING_PAGE,
    111    gBrowser,
    112  });
    113  let [tab] = await Promise.all([promise, waitForContentBlockingEvent()]);
    114 
    115  if (hasException) {
    116    let loaded = BrowserTestUtils.browserLoaded(
    117      tab.linkedBrowser,
    118      false,
    119      TRACKING_PAGE
    120    );
    121    gProtectionsHandler.disableForCurrentPage();
    122    await loaded;
    123  }
    124 
    125  promise = waitForContentBlockingEvent();
    126  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    127    content.postMessage("socialtracking", "*");
    128  });
    129  await promise;
    130 
    131  await openProtectionsPanel();
    132 
    133  let categoryItem = document.getElementById(
    134    "protections-popup-category-socialblock"
    135  );
    136 
    137  // Explicitly waiting for the category item becoming visible.
    138  await TestUtils.waitForCondition(() => {
    139    return BrowserTestUtils.isVisible(categoryItem);
    140  });
    141 
    142  ok(BrowserTestUtils.isVisible(categoryItem), "STP category item is visible");
    143  ok(
    144    categoryItem.classList.contains("blocked"),
    145    "STP category item is blocked"
    146  );
    147 
    148  /* eslint-disable mozilla/no-arbitrary-setTimeout */
    149  // We have to wait until the ContentBlockingLog gets updated in the content.
    150  // Unfortunately, we need to use the setTimeout here since we don't have an
    151  // easy to know whether the log is updated in the content. This should be
    152  // removed after the log been removed in the content (Bug 1599046).
    153  await new Promise(resolve => {
    154    setTimeout(resolve, 500);
    155  });
    156  /* eslint-enable mozilla/no-arbitrary-setTimeout */
    157 
    158  let subview = document.getElementById("protections-popup-socialblockView");
    159  let viewShown = BrowserTestUtils.waitForEvent(subview, "ViewShown");
    160  categoryItem.click();
    161  await viewShown;
    162 
    163  let trackersViewShimHint = document.getElementById(
    164    "protections-popup-socialblockView-shim-allow-hint"
    165  );
    166  ok(trackersViewShimHint.hidden, "Shim hint is hidden");
    167 
    168  let listItems = subview.querySelectorAll(".protections-popup-list-item");
    169  is(listItems.length, 1, "We have 1 item in the list");
    170  let listItem = listItems[0];
    171  ok(BrowserTestUtils.isVisible(listItem), "List item is visible");
    172  is(
    173    listItem.querySelector("label").value,
    174    "https://social-tracking.example.org",
    175    "Has the correct host"
    176  );
    177 
    178  let mainView = document.getElementById("protections-popup-mainView");
    179  viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
    180  let backButton = subview.querySelector(".subviewbutton-back");
    181  backButton.click();
    182  await viewShown;
    183 
    184  ok(true, "Main view was shown");
    185 
    186  if (hasException) {
    187    let loaded = BrowserTestUtils.browserLoaded(
    188      tab.linkedBrowser,
    189      false,
    190      TRACKING_PAGE
    191    );
    192    gProtectionsHandler.enableForCurrentPage();
    193    await loaded;
    194  }
    195 
    196  BrowserTestUtils.removeTab(tab);
    197 }
    198 
    199 async function testCategoryItem(blockLoads) {
    200  if (blockLoads) {
    201    Services.prefs.setBoolPref(ST_PROTECTION_PREF, true);
    202  }
    203 
    204  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, false);
    205 
    206  let promise = BrowserTestUtils.openNewForegroundTab({
    207    url: TRACKING_PAGE,
    208    gBrowser,
    209  });
    210  let [tab] = await Promise.all([promise, waitForContentBlockingEvent()]);
    211 
    212  await openProtectionsPanel();
    213 
    214  let categoryItem = document.getElementById(
    215    "protections-popup-category-socialblock"
    216  );
    217 
    218  let noTrackersDetectedDesc = document.getElementById(
    219    "protections-popup-no-trackers-found-description"
    220  );
    221 
    222  ok(categoryItem.hasAttribute("uidisabled"), "Category should be uidisabled");
    223 
    224  ok(
    225    !categoryItem.classList.contains("blocked"),
    226    "Category not marked as blocked"
    227  );
    228  ok(!BrowserTestUtils.isVisible(categoryItem), "Item should be hidden");
    229  ok(
    230    !gProtectionsHandler._protectionsPopup.hasAttribute("detected"),
    231    "trackers are not detected"
    232  );
    233 
    234  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    235    content.postMessage("socialtracking", "*");
    236  });
    237 
    238  ok(
    239    !categoryItem.classList.contains("blocked"),
    240    "Category not marked as blocked"
    241  );
    242  ok(!BrowserTestUtils.isVisible(categoryItem), "Item should be hidden");
    243  ok(
    244    !gProtectionsHandler._protectionsPopup.hasAttribute("detected"),
    245    "trackers are not detected"
    246  );
    247  ok(
    248    BrowserTestUtils.isVisible(noTrackersDetectedDesc),
    249    "No Trackers detected should be shown"
    250  );
    251 
    252  BrowserTestUtils.removeTab(tab);
    253 
    254  Services.prefs.setBoolPref(ST_BLOCK_COOKIES_PREF, true);
    255 
    256  promise = BrowserTestUtils.openNewForegroundTab({
    257    url: TRACKING_PAGE,
    258    gBrowser,
    259  });
    260  [tab] = await Promise.all([promise, waitForContentBlockingEvent()]);
    261 
    262  await openProtectionsPanel();
    263 
    264  ok(!categoryItem.hasAttribute("uidisabled"), "Item shouldn't be uidisabled");
    265 
    266  ok(categoryItem.classList.contains("blocked"), "Category marked as blocked");
    267  ok(
    268    categoryItem.classList.contains("notFound"),
    269    "Category marked as not found"
    270  );
    271  // At this point we should still be showing "No Trackers Detected"
    272  ok(!BrowserTestUtils.isVisible(categoryItem), "Item should not be visible");
    273  ok(
    274    BrowserTestUtils.isVisible(noTrackersDetectedDesc),
    275    "No Trackers detected should be shown"
    276  );
    277  ok(
    278    !gProtectionsHandler._protectionsPopup.hasAttribute("detected"),
    279    "trackers are not detected"
    280  );
    281 
    282  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    283    content.postMessage("socialtracking", "*");
    284  });
    285 
    286  await TestUtils.waitForCondition(() => {
    287    return !categoryItem.classList.contains("notFound");
    288  });
    289 
    290  ok(categoryItem.classList.contains("blocked"), "Category marked as blocked");
    291  ok(
    292    !categoryItem.classList.contains("notFound"),
    293    "Category not marked as not found"
    294  );
    295  ok(BrowserTestUtils.isVisible(categoryItem), "Item should be visible");
    296  ok(
    297    !BrowserTestUtils.isVisible(noTrackersDetectedDesc),
    298    "No Trackers detected should be hidden"
    299  );
    300  ok(
    301    gProtectionsHandler._protectionsPopup.hasAttribute("detected"),
    302    "trackers are not detected"
    303  );
    304 
    305  BrowserTestUtils.removeTab(tab);
    306 
    307  Services.prefs.clearUserPref(ST_PROTECTION_PREF);
    308 }
    309 
    310 add_task(async function testIdentityUI() {
    311  requestLongerTimeout(2);
    312 
    313  await testIdentityState(false);
    314  await testIdentityState(true);
    315 
    316  await testSubview(false);
    317  await testSubview(true);
    318 
    319  await testCategoryItem(false);
    320  await testCategoryItem(true);
    321 });