summaryrefslogtreecommitdiffstats
path: root/src/tests/tests/tabData.js
blob: c578cd128273e715bc10844d2ab85cf066b93abb (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* globals badger:false */

(function () {

let constants = require('constants');

QUnit.module("tabData", {
  beforeEach: function () {

    this.SITE_URL = "http://example.com/";
    this.tabId = 9999;

    badger.recordFrame(this.tabId, 0, this.SITE_URL);

    // stub chrome.tabs.get manually as we have some sort of issue stubbing with Sinon in Firefox
    this.chromeTabsGet = chrome.tabs.get;
    chrome.tabs.get = (tab_id, callback) => {
      return callback({
        active: true
      });
    };
  },

  afterEach: function () {
    chrome.tabs.get = this.chromeTabsGet;
    delete badger.tabData[this.tabId];
  }
},
function() {
  QUnit.module("logThirdPartyOriginOnTab", {
    beforeEach: function () {
      this.clock = sinon.useFakeTimers();
      sinon.stub(chrome.browserAction, "setBadgeText");
    },
    afterEach: function () {
      chrome.browserAction.setBadgeText.restore();
      this.clock.restore();
    },
  });

  QUnit.test("logging blocked domain", function (assert) {
    const DOMAIN = "example.com";

    assert.equal(
      badger.getTrackerCount(this.tabId), 0, "count starts at zero"
    );

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN, constants.BLOCK);

    // log blocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge gets called when we see a blocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "1"
    }), "setBadgeText was called with expected args");
  });

  QUnit.test("logging unblocked domain", function (assert) {
    badger.logThirdPartyOriginOnTab(this.tabId, "example.com", constants.ALLOW);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 0, "count stays at zero"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.notCalled,
      "updateBadge does not get called when we see a hasn't-decided-yet-to-block domain"
    );
  });

  QUnit.test("logging DNT-compliant domain", function (assert) {
    badger.logThirdPartyOriginOnTab(this.tabId, "example.com", constants.DNT);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 0, "count stays at zero"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.notCalled,
      "updateBadge does not get called when we see a DNT-compliant domain"
    );
  });

  QUnit.test("logging as unblocked then as blocked", function (assert) {
    const DOMAIN = "example.com";

    // log unblocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.ALLOW);
    this.clock.tick(1);

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN, constants.BLOCK);

    // log the same domain, this time as blocked
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.equal(
      chrome.browserAction.setBadgeText.callCount,
      "1",
      "updateBadge gets called when we see a blocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "1"
    }), "setBadgeText was called with expected args");
  });

  QUnit.test("logging blocked domain twice", function (assert) {
    const DOMAIN = "example.com";

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN, constants.BLOCK);

    // log blocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge gets called when we see a blocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "1"
    }), "setBadgeText was called with expected args");

    // log the same blocked domain again
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId),
      1,
      "count does not get incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge not called when we see the same blocked domain again"
    );
  });

  QUnit.test("logging 2x unblocked then 2x blocked", function (assert) {
    const DOMAIN = "example.com";

    // log unblocked domain twice
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.ALLOW);
    this.clock.tick(1);
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.ALLOW);
    this.clock.tick(1);

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN, constants.BLOCK);

    // log blocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge gets called when we see a blocked domain"
    );
    assert.deepEqual(chrome.browserAction.setBadgeText.getCall(0).args[0], {
      tabId: this.tabId,
      text: "1"
    }, "setBadgeText was called with expected args");

    // log the same blocked domain again
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId),
      1,
      "count does not get incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge not called when we see the same blocked domain again"
    );
  });

  QUnit.test("logging cookieblocked domain", function (assert) {
    const DOMAIN = "example.com";

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN, constants.COOKIEBLOCK);

    // log cookieblocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN, constants.COOKIEBLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge gets called when we see a cookieblocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "1"
    }), "setBadgeText was called with expected args");
  });

  QUnit.test("logging several domains", function (assert) {
    const DOMAIN1 = "example.com",
      DOMAIN2 = "example.net";

    // set up domain blocking (used by getTrackerCount)
    badger.storage.setupHeuristicAction(DOMAIN1, constants.BLOCK);
    badger.storage.setupHeuristicAction(DOMAIN2, constants.COOKIEBLOCK);

    // log blocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN1, constants.BLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 1, "count gets incremented"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledOnce,
      "updateBadge gets called when we see a blocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "1"
    }), "setBadgeText was called with expected args");

    // log cookieblocked domain
    badger.logThirdPartyOriginOnTab(this.tabId, DOMAIN2, constants.COOKIEBLOCK);
    this.clock.tick(1);
    assert.equal(
      badger.getTrackerCount(this.tabId), 2, "count gets incremented again"
    );
    assert.ok(
      chrome.browserAction.setBadgeText.calledTwice,
      "updateBadge gets called when we see a cookieblocked domain"
    );
    assert.ok(chrome.browserAction.setBadgeText.calledWithExactly({
      tabId: this.tabId,
      text: "2"
    }), "setBadgeText was called with expected args");
  });

  QUnit.module('updateBadge', {
    beforeEach: function() {
      this.setBadgeText = sinon.stub(chrome.browserAction, "setBadgeText");

      // another Firefox workaround: setBadgeText gets stubbed fine but setBadgeBackgroundColor doesn't
      this.setBadgeBackgroundColor = chrome.browserAction.setBadgeBackgroundColor;
    },
    afterEach: function() {
      this.setBadgeText.restore();
      chrome.browserAction.setBadgeBackgroundColor = this.setBadgeBackgroundColor;
    },
  });

  QUnit.test("disabled", function(assert) {
    let done = assert.async(2),
      called = false;

    badger.disablePrivacyBadgerForOrigin(window.extractHostFromURL(this.SITE_URL));

    this.setBadgeText.callsFake((obj) => {
      assert.deepEqual(obj, {tabId: this.tabId, text: ''});
      done();
    });
    chrome.browserAction.setBadgeBackgroundColor = () => {called = true;};

    badger.updateBadge(this.tabId);

    assert.notOk(called, "setBadgeBackgroundColor does not get called");

    done();
  });

  QUnit.test("numblocked zero", function(assert) {
    let done = assert.async(2),
      called = false;

    this.setBadgeText.callsFake((obj) => {
      assert.deepEqual(
        obj,
        {tabId: this.tabId, text: ""},
        "setBadgeText called with expected args"
      );
      done();
    });
    chrome.browserAction.setBadgeBackgroundColor = () => {called = true;};

    badger.updateBadge(this.tabId);

    assert.notOk(called, "setBadgeBackgroundColor does not get called");

    done();
  });

});

}());