tor-browser

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

browser_protectionsUI_telemetry.js (3209B)


      1 /*
      2 * Test telemetry for Tracking Protection
      3 */
      4 
      5 const PREF = "privacy.trackingprotection.enabled";
      6 const BENIGN_PAGE =
      7  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      8  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/benignPage.html";
      9 const TRACKING_PAGE =
     10  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     11  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
     12 
     13 /**
     14 * Enable local telemetry recording for the duration of the tests.
     15 */
     16 var oldCanRecord = Services.telemetry.canRecordExtended;
     17 Services.telemetry.canRecordExtended = true;
     18 registerCleanupFunction(function () {
     19  UrlClassifierTestUtils.cleanupTestTrackers();
     20  Services.telemetry.canRecordExtended = oldCanRecord;
     21  Services.prefs.clearUserPref(PREF);
     22 });
     23 
     24 function getShieldHistogram() {
     25  return Services.telemetry.getHistogramById("TRACKING_PROTECTION_SHIELD");
     26 }
     27 
     28 function getShieldCounts() {
     29  return getShieldHistogram().snapshot().values;
     30 }
     31 
     32 add_setup(async function () {
     33  await UrlClassifierTestUtils.addTestTrackers();
     34 
     35  let TrackingProtection =
     36    gBrowser.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection;
     37  ok(TrackingProtection, "TP is attached to the browser window");
     38  ok(!TrackingProtection.enabled, "TP is not enabled");
     39 
     40  let enabledCounts = Services.telemetry
     41    .getHistogramById("TRACKING_PROTECTION_ENABLED")
     42    .snapshot().values;
     43  is(enabledCounts[0], 1, "TP was not enabled on start up");
     44 });
     45 
     46 add_task(async function testShieldHistogram() {
     47  Services.prefs.setBoolPref(PREF, true);
     48  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
     49 
     50  // Reset these to make counting easier
     51  getShieldHistogram().clear();
     52 
     53  await BrowserTestUtils.loadURIString({
     54    browser: tab.linkedBrowser,
     55    uriString: BENIGN_PAGE,
     56  });
     57  is(getShieldCounts()[0], 1, "Page loads without tracking");
     58 
     59  await BrowserTestUtils.loadURIString({
     60    browser: tab.linkedBrowser,
     61    uriString: TRACKING_PAGE,
     62  });
     63  is(getShieldCounts()[0], 2, "Adds one more page load");
     64  is(getShieldCounts()[2], 1, "Counts one instance of the shield being shown");
     65 
     66  info("Disable TP for the page (which reloads the page)");
     67  let reloadURI = tab.linkedBrowser.currentURI.spec;
     68  let tabReloadPromise = BrowserTestUtils.loadURIString({
     69    browser: tab.linkedBrowser,
     70    uriString: reloadURI,
     71  });
     72  gProtectionsHandler.disableForCurrentPage();
     73  await tabReloadPromise;
     74  is(getShieldCounts()[0], 3, "Adds one more page load");
     75  is(
     76    getShieldCounts()[1],
     77    1,
     78    "Counts one instance of the shield being crossed out"
     79  );
     80 
     81  info("Re-enable TP for the page (which reloads the page)");
     82  reloadURI = tab.linkedBrowser.currentURI.spec;
     83  tabReloadPromise = BrowserTestUtils.loadURIString({
     84    browser: tab.linkedBrowser,
     85    uriString: reloadURI,
     86  });
     87  gProtectionsHandler.enableForCurrentPage();
     88  await tabReloadPromise;
     89  is(getShieldCounts()[0], 4, "Adds one more page load");
     90  is(
     91    getShieldCounts()[2],
     92    2,
     93    "Adds one more instance of the shield being shown"
     94  );
     95 
     96  gBrowser.removeCurrentTab();
     97 
     98  // Reset these to make counting easier for the next test
     99  getShieldHistogram().clear();
    100 });