summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_donottrack.html
blob: 56003e7eb60d7ce487ff1f89bd97e81915e4e6fe (plain)
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
147
148
149
150
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1258033 - Fix the DNT loophole for tracking protection</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);

const tests = [
  // DNT turned on and TP turned off, DNT signal sent in both private browsing
  // and normal mode.
  {
    setting:  {dntPref:true, tpPref:false, tppbPref:false, pbMode:true},
    expected: {dnt: "1"},
  },
  {
    setting:  {dntPref:true,  tpPref:false, tppbPref:false, pbMode:false},
    expected: {dnt: "1"}
  },
  // DNT turned off and TP turned on globally, DNT signal sent in both private
  // browsing and normal mode.
  {
    setting:  {dntPref:false, tpPref:true,  tppbPref:false, pbMode:true},
    expected: {dnt: "1"}
  },
  {
    setting:  {dntPref:false, tpPref:true,  tppbPref:false, pbMode:false},
    expected: {dnt: "1"}
  },
  // DNT turned off and TP in Private Browsing only, DNT signal sent in private
  // browsing mode only.
  {
    setting:  {dntPref:false, tpPref:false, tppbPref:true,  pbMode:true},
    expected: {dnt: "1"}
  },
  {
    setting:  {dntPref:false, tpPref:false, tppbPref:true,  pbMode:false},
    expected: {dnt: "unspecified"}
  },
  // DNT turned off and TP turned off, DNT signal is never sent.
  {
    setting:  {dntPref:false, tpPref:false, tppbPref:false, pbMode:true},
    expected: {dnt: "unspecified"}
  },
  {
    setting:  {dntPref:false, tpPref:false, tppbPref:false, pbMode:false},
    expected: {dnt: "unspecified"}
  },
]

const DNT_PREF = 'privacy.donottrackheader.enabled';
const TP_PREF = 'privacy.trackingprotection.enabled';
const TP_PB_PREF = 'privacy.trackingprotection.pbmode.enabled';

const contentPage =
  "http://mochi.test:8888/tests/toolkit/components/url-classifier/tests/mochitest/dnt.html";

Components.utils.import("resource://gre/modules/Services.jsm");

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
      setTimeout(aCallback, 0);
    }
  }, "browser-delayed-startup-finished", false);
}

function executeTest(test) {
  SpecialPowers.pushPrefEnv({"set" : [
   [DNT_PREF, test.setting.dntPref],
   [TP_PREF, test.setting.tpPref],
   [TP_PB_PREF, test.setting.tppbPref]
  ]});

  var win = mainWindow.OpenBrowserWindow({private: test.setting.pbMode});

  return new Promise(function(resolve, reject) {
    win.addEventListener("load", function onLoad() {
      win.removeEventListener("load", onLoad, false);
      whenDelayedStartupFinished(win, function() {
        win.addEventListener("DOMContentLoaded", function onInnerLoad() {
          if (win.content.location.href != contentPage) {
            win.gBrowser.loadURI(contentPage);
            return;
          }

          win.removeEventListener("DOMContentLoaded", onInnerLoad, true);

          win.content.addEventListener('message', function (event) {
            let [key, value] = event.data.split("=");
            if (key == "finish") {
              win.close();
              resolve();
            } else if (key == "navigator.doNotTrack") {
              is(value, test.expected.dnt, "navigator.doNotTrack should be " + test.expected.dnt);
            } else if (key == "DNT") {
              let msg = test.expected.dnt == "1" ? "" : "not ";
              is(value, test.expected.dnt, "DNT header should " + msg + "be sent");
            } else {
              ok(false, "unexpected message");
            }
          });
        }, true);
        SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
      });
    }, true);
  });
}

let loop = function loop(index) {
  if (index >= tests.length) {
    SimpleTest.finish();
    return;
  }

  let test = tests[index];
  let next = function next() {
    loop(index + 1);
  };
  let result = executeTest(test);
  result.then(next, next);
};

SimpleTest.waitForExplicitFinish();
loop(0);

</script>

</pre>
</body>
</html>