summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/robocop/testTrackingProtection.js
blob: d81efd6c663e78eb2bf9354ebb7ca448937bb873 (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
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Messaging.jsm");

function promiseLoadEvent(browser, url, eventType="load", runBeforeLoad) {
  return new Promise((resolve, reject) => {
    do_print("Wait browser event: " + eventType);

    function handle(event) {
      if (event.target != browser.contentDocument || event.target.location.href == "about:blank" || (url && event.target.location.href != url)) {
        do_print("Skipping spurious '" + eventType + "' event" + " for " + event.target.location.href);
        return;
      }

      browser.removeEventListener(eventType, handle, true);
      do_print("Browser event received: " + eventType);
      resolve(event);
    }

    browser.addEventListener(eventType, handle, true);

    if (runBeforeLoad) {
      runBeforeLoad();
    }
    if (url) {
      browser.loadURI(url);
    }
  });
}

// Test that the Tracking Protection is active and has the correct state when
// tracking content is blocked (Bug 1063831)

// Code is mostly stolen from:
// http://dxr.mozilla.org/mozilla-central/source/browser/base/content/test/general/browser_trackingUI.js

var TABLE = "urlclassifier.trackingTable";

// Update tracking database
function doUpdate() {
  // Add some URLs to the tracking database (to be blocked)
  var testData = "tracking.example.com/";
  var testUpdate =
    "n:1000\ni:test-track-simple\nad:1\n" +
    "a:524:32:" + testData.length + "\n" +
    testData;

  let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService);

  return new Promise((resolve, reject) => {
    let listener = {
      QueryInterface: function(iid) {
        if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIUrlClassifierUpdateObserver))
          return this;

        throw Cr.NS_ERROR_NO_INTERFACE;
      },
      updateUrlRequested: function(url) { },
      streamFinished: function(status) { },
      updateError: function(errorCode) {
        ok(false, "Couldn't update classifier.");
        resolve();
      },
      updateSuccess: function(requestedTimeout) {
        resolve();
      }
    };

    dbService.beginUpdate(listener, "test-track-simple", "");
    dbService.beginStream("", "");
    dbService.updateStream(testUpdate);
    dbService.finishStream();
    dbService.finishUpdate();
  });
}

var BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;

// Tests the tracking protection UI in private browsing. By default, tracking protection is
// enabled in private browsing ("privacy.trackingprotection.pbmode.enabled").
add_task(function* test_tracking_pb() {
  // Load a blank page
  let browser = BrowserApp.addTab("about:blank", { selected: true, parentId: BrowserApp.selectedTab.id, isPrivate: true }).browser;
  yield new Promise((resolve, reject) => {
    browser.addEventListener("load", function startTests(event) {
      browser.removeEventListener("load", startTests, true);
      Services.tm.mainThread.dispatch(resolve, Ci.nsIThread.DISPATCH_NORMAL);
    }, true);
  });

  // Populate and use 'test-track-simple' for tracking protection lookups
  Services.prefs.setCharPref(TABLE, "test-track-simple");
  yield doUpdate();

  // Point tab to a test page NOT containing tracking elements
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_good.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "unknown" });

  // Point tab to a test page containing tracking elements
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_bad.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "tracking_content_blocked" });

  // Simulate a click on the "Disable protection" button in the site identity popup.
  // We need to wait for a "load" event because "Session:Reload" will cause a full page reload.
  yield promiseLoadEvent(browser, undefined, undefined, () => {
    Services.obs.notifyObservers(null, "Session:Reload", "{\"allowContent\":true,\"contentType\":\"tracking\"}");
  });
  Messaging.sendRequest({ type: "Test:Expected", expected: "tracking_content_loaded" });

  // Simulate a click on the "Enable protection" button in the site identity popup.
  yield promiseLoadEvent(browser, undefined, undefined, () => {
    Services.obs.notifyObservers(null, "Session:Reload", "{\"allowContent\":false,\"contentType\":\"tracking\"}");
  });
  Messaging.sendRequest({ type: "Test:Expected", expected: "tracking_content_blocked" });

  // Disable tracking protection to make sure we don't show the UI when the pref is disabled.
  Services.prefs.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);

  // Point tab to a test page containing tracking elements
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_bad.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "unknown" });

  // Point tab to a test page NOT containing tracking elements
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_good.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "unknown" });

  // Reset the pref before the next testcase
  Services.prefs.clearUserPref("privacy.trackingprotection.pbmode.enabled");
});

add_task(function* test_tracking_not_pb() {
  // Load a blank page
  let browser = BrowserApp.addTab("about:blank", { selected: true }).browser;
  yield new Promise((resolve, reject) => {
    browser.addEventListener("load", function startTests(event) {
      browser.removeEventListener("load", startTests, true);
      Services.tm.mainThread.dispatch(resolve, Ci.nsIThread.DISPATCH_NORMAL);
    }, true);
  });

  // Point tab to a test page NOT containing tracking elements
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_good.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "unknown" });

  // Point tab to a test page containing tracking elements (tracking protection UI *should not* be shown)
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_bad.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "unknown" });

  // Enable tracking protection in normal tabs
  Services.prefs.setBoolPref("privacy.trackingprotection.enabled", true);

  // Point tab to a test page containing tracking elements (tracking protection UI *should* be shown)
  yield promiseLoadEvent(browser, "http://tracking.example.org/tests/robocop/tracking_bad.html");
  Messaging.sendRequest({ type: "Test:Expected", expected: "tracking_content_blocked" });
});

run_next_test();