File: test_privatebrowsing_trackingprotection.html

package info (click to toggle)
thunderbird 1%3A60.9.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,492 kB
  • sloc: cpp: 5,457,040; ansic: 2,360,385; python: 596,167; asm: 340,963; java: 326,296; xml: 258,830; sh: 84,445; makefile: 23,705; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (146 lines) | stat: -rw-r--r-- 5,552 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!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 Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;

var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);
var contentPage = "http://www.itisatrap.org/tests/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedPBFrame.html";

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

function testOnWindow(aPrivate, aCallback) {
  var 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);
          return;
        }
        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);

        win.content.addEventListener("load", function innerLoad2() {
          win.content.removeEventListener("load", innerLoad2);
          SimpleTest.executeSoon(function() { aCallback(win); });
        }, false, true);
      }, true);
      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
    });
  }, {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");

  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.blockedTrackingNodeCount, aBlocked ? badids.length : 0, "Should identify all tracking elements");

  var blockedTrackingNodes = win.document.blockedTrackingNodes;

  // Make sure that every node in blockedTrackingNodes 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 < blockedTrackingNodes.length; i++) {
    let nodeMatch = false;
    for (let j = 0; j < badids.length && !nodeMatch; j++) {
      nodeMatch = nodeMatch ||
        (blockedTrackingNodes[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
  // blockedTrackingNodes. 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 < blockedTrackingNodes.length && !nodeMatch; i++) {
      nodeMatch = nodeMatch ||
        (blockedTrackingNodes[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": [["urlclassifier.trackingTable", "test-track-simple"],
            ["privacy.trackingprotection.enabled", false],
            ["privacy.trackingprotection.pbmode.enabled", true],
            ["channelclassifier.allowlist_example", true]]},
  test);

function test() {
  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
  UrlClassifierTestUtils.addTestTrackers().then(() => {
    // Normal mode, with the pref (trackers should be loaded)
    testOnWindow(false, function(aWindow) {
      checkLoads(aWindow, false);
      aWindow.close();

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

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

SimpleTest.waitForExplicitFinish();

</script>

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