toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
author Andrea Marchesini <amarchesini@mozilla.com>
Wed, 30 Jan 2019 19:08:08 +0000
changeset 513998 00494efd99e7935d1de77aaa2aa62467d706fa05
parent 513677 6b56696d713a7f7858f16235e37baa8307e73b49
child 514009 7def006b54215a2dc66248504286db39f981ff95
permissions -rw-r--r--
Bug 1522210 - Fingerprinting and cryptomining classifiers must have separate nsIWebProgressListener blocking state codes - part 1 - UrlClassifierFeatureFactory::IsClassifierBlockingErrorCode, r=Ehsan Differential Revision: https://phabricator.services.mozilla.com/D17637

<!DOCTYPE HTML>
<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
  <title>Test Tracking Protection in Private Browsing mode</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>

<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">

<script class="testbody" type="text/javascript">

var mainWindow = window.docShell.rootTreeItem.domWindow;
var contentPage = "http://www.itisatrap.org/chrome/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedPBFrame.html";

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {UrlClassifierTestUtils} = ChromeUtils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
const {TestUtils} = ChromeUtils.import("resource://testing-common/TestUtils.jsm");

function testOnWindow(aPrivate) {
  return new Promise((resolve, reject) => {
    let win = mainWindow.OpenBrowserWindow({private: aPrivate});
    win.addEventListener("load", function() {
      TestUtils.topicObserved("browser-delayed-startup-finished",
                              subject => subject == win).then(() => {
        win.addEventListener("DOMContentLoaded", function onInnerLoad() {
          if (win.content.location.href != contentPage) {
            win.gBrowser.loadURI(contentPage, {
              triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
            });
            return;
          }
          win.removeEventListener("DOMContentLoaded", onInnerLoad, true);

          win.content.addEventListener("load", function innerLoad2() {
            win.content.removeEventListener("load", innerLoad2);
            SimpleTest.executeSoon(function() {
              resolve(win);
            });
          }, false, true);
        }, true);
        SimpleTest.executeSoon(function() {
          win.gBrowser.loadURI(contentPage, {
            triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
          });
        });
      });
    }, {capture: true, once: true});
  });
}

var badids = [
  "badscript",
  "badimage",
  "badcss",
];

function checkLoads(aWindow, aBlocked) {
  var win = aWindow.content;
  is(win.document.getElementById("badscript").dataset.touched, aBlocked ? "no" : "yes", "Should not load tracking javascript");
  is(win.document.getElementById("badimage").dataset.touched, aBlocked ? "no" : "yes", "Should not load tracking images");
  is(win.document.getElementById("goodscript").dataset.touched, "yes", "Should load whitelisted tracking javascript");
  is(win.document.getElementById("goodimage").dataset.touched, "yes", "Should load non-blacklisted image");

  var elt = win.document.getElementById("styleCheck");
  var style = win.document.defaultView.getComputedStyle(elt);
  isnot(style.visibility, aBlocked ? "hidden" : "", "Should not load tracking css");

  is(win.document.blockedNodeByClassifierCount, aBlocked ? badids.length : 0, "Should identify all tracking elements");

  var blockedNodes = win.document.blockedNodesByClassifier;

  // Make sure that every node in blockedNodes exists in the tree
  // (that may not always be the case but do not expect any nodes to disappear
  // from the tree here)
  var allNodeMatch = true;
  for (let i = 0; i < blockedNodes.length; i++) {
    let nodeMatch = false;
    for (let j = 0; j < badids.length && !nodeMatch; j++) {
      nodeMatch = nodeMatch ||
        (blockedNodes[i] == win.document.getElementById(badids[j]));
    }

    allNodeMatch = allNodeMatch && nodeMatch;
  }
  is(allNodeMatch, true, "All annotated nodes are expected in the tree");

  // Make sure that every node with a badid (see badids) is found in the
  // blockedNodes. This tells us if we are neglecting to annotate
  // some nodes
  allNodeMatch = true;
  for (let j = 0; j < badids.length; j++) {
    let nodeMatch = false;
    for (let i = 0; i < blockedNodes.length && !nodeMatch; i++) {
      nodeMatch = nodeMatch ||
        (blockedNodes[i] == win.document.getElementById(badids[j]));
    }

    allNodeMatch = allNodeMatch && nodeMatch;
  }
  is(allNodeMatch, aBlocked, "All tracking nodes are expected to be annotated as such");
}

SpecialPowers.pushPrefEnv(
  {"set": [
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", true],
  ]}, test);

async function test() {
  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
  await UrlClassifierTestUtils.addTestTrackers();

  // Normal mode, with the pref (trackers should be loaded)
  await testOnWindow(false).then(function(aWindow) {
    checkLoads(aWindow, false);
    aWindow.close();
  });

  // Private Browsing, with the pref (trackers should be blocked)
  await testOnWindow(true).then(function(aWindow) {
    checkLoads(aWindow, true);
    aWindow.close();
  });

  // Private Browsing, without the pref (trackers should be loaded)
  await SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
  await testOnWindow(true).then(function(aWindow) {
    checkLoads(aWindow, false);
    aWindow.close();
  });

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();

</script>

</pre>
<iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
</body>
</html>