addon-sdk/source/test/private-browsing/global.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 const timer = require("sdk/timers");
michael@0 7 const { LoaderWithHookedConsole, deactivate, pb, pbUtils } = require("./helper");
michael@0 8 const tabs = require("sdk/tabs");
michael@0 9 const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
michael@0 10 const { set: setPref } = require("sdk/preferences/service");
michael@0 11 const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
michael@0 12
michael@0 13 exports["test activate private mode via handler"] = function(assert, done) {
michael@0 14 function onReady(tab) {
michael@0 15 if (tab.url == "about:robots")
michael@0 16 tab.close(function() pb.activate());
michael@0 17 }
michael@0 18 function cleanup(tab) {
michael@0 19 if (tab.url == "about:") {
michael@0 20 tabs.removeListener("ready", cleanup);
michael@0 21 tab.close(function onClose() {
michael@0 22 done();
michael@0 23 });
michael@0 24 }
michael@0 25 }
michael@0 26
michael@0 27 tabs.on("ready", onReady);
michael@0 28 pb.once("start", function onStart() {
michael@0 29 assert.pass("private mode was activated");
michael@0 30 pb.deactivate();
michael@0 31 });
michael@0 32 pb.once("stop", function onStop() {
michael@0 33 assert.pass("private mode was deactivated");
michael@0 34 tabs.removeListener("ready", onReady);
michael@0 35 tabs.on("ready", cleanup);
michael@0 36 });
michael@0 37 tabs.once("open", function onOpen() {
michael@0 38 tabs.open("about:robots");
michael@0 39 });
michael@0 40 tabs.open("about:");
michael@0 41 };
michael@0 42
michael@0 43 // tests that isActive has the same value as the private browsing service
michael@0 44 // expects
michael@0 45 exports.testGetIsActive = function (assert) {
michael@0 46 assert.equal(pb.isActive, false,
michael@0 47 "private-browsing.isActive is correct without modifying PB service");
michael@0 48 assert.equal(pb.isPrivate(), false,
michael@0 49 "private-browsing.sPrivate() is correct without modifying PB service");
michael@0 50
michael@0 51 pb.once("start", function() {
michael@0 52 assert.ok(pb.isActive,
michael@0 53 "private-browsing.isActive is correct after modifying PB service");
michael@0 54 assert.ok(pb.isPrivate(),
michael@0 55 "private-browsing.sPrivate() is correct after modifying PB service");
michael@0 56 // Switch back to normal mode.
michael@0 57 pb.deactivate();
michael@0 58 });
michael@0 59 pb.activate();
michael@0 60
michael@0 61 pb.once("stop", function() {
michael@0 62 assert.ok(!pb.isActive,
michael@0 63 "private-browsing.isActive is correct after modifying PB service");
michael@0 64 assert.ok(!pb.isPrivate(),
michael@0 65 "private-browsing.sPrivate() is correct after modifying PB service");
michael@0 66 test.done();
michael@0 67 });
michael@0 68 };
michael@0 69
michael@0 70 exports.testStart = function(assert, done) {
michael@0 71 pb.on("start", function onStart() {
michael@0 72 assert.equal(this, pb, "`this` should be private-browsing module");
michael@0 73 assert.ok(pbUtils.getMode(),
michael@0 74 'private mode is active when "start" event is emitted');
michael@0 75 assert.ok(pb.isActive,
michael@0 76 '`isActive` is `true` when "start" event is emitted');
michael@0 77 assert.ok(pb.isPrivate(),
michael@0 78 '`isPrivate` is `true` when "start" event is emitted');
michael@0 79 pb.removeListener("start", onStart);
michael@0 80 deactivate(done);
michael@0 81 });
michael@0 82 pb.activate();
michael@0 83 };
michael@0 84
michael@0 85 exports.testStop = function(assert, done) {
michael@0 86 pb.once("stop", function onStop() {
michael@0 87 assert.equal(this, pb, "`this` should be private-browsing module");
michael@0 88 assert.equal(pbUtils.getMode(), false,
michael@0 89 "private mode is disabled when stop event is emitted");
michael@0 90 assert.equal(pb.isActive, false,
michael@0 91 "`isActive` is `false` when stop event is emitted");
michael@0 92 assert.equal(pb.isPrivate(), false,
michael@0 93 "`isPrivate()` is `false` when stop event is emitted");
michael@0 94 done();
michael@0 95 });
michael@0 96 pb.activate();
michael@0 97 pb.once("start", function() {
michael@0 98 pb.deactivate();
michael@0 99 });
michael@0 100 };
michael@0 101
michael@0 102 exports.testBothListeners = function(assert, done) {
michael@0 103 let stop = false;
michael@0 104 let start = false;
michael@0 105
michael@0 106 function onStop() {
michael@0 107 assert.equal(stop, false,
michael@0 108 "stop callback must be called only once");
michael@0 109 assert.equal(pbUtils.getMode(), false,
michael@0 110 "private mode is disabled when stop event is emitted");
michael@0 111 assert.equal(pb.isActive, false,
michael@0 112 "`isActive` is `false` when stop event is emitted");
michael@0 113 assert.equal(pb.isPrivate(), false,
michael@0 114 "`isPrivate()` is `false` when stop event is emitted");
michael@0 115
michael@0 116 pb.on("start", finish);
michael@0 117 pb.removeListener("start", onStart);
michael@0 118 pb.removeListener("start", onStart2);
michael@0 119 pb.activate();
michael@0 120 stop = true;
michael@0 121 }
michael@0 122
michael@0 123 function onStart() {
michael@0 124 assert.equal(false, start,
michael@0 125 "stop callback must be called only once");
michael@0 126 assert.ok(pbUtils.getMode(),
michael@0 127 "private mode is active when start event is emitted");
michael@0 128 assert.ok(pb.isActive,
michael@0 129 "`isActive` is `true` when start event is emitted");
michael@0 130 assert.ok(pb.isPrivate(),
michael@0 131 "`isPrivate()` is `true` when start event is emitted");
michael@0 132
michael@0 133 pb.on("stop", onStop);
michael@0 134 pb.deactivate();
michael@0 135 start = true;
michael@0 136 }
michael@0 137
michael@0 138 function onStart2() {
michael@0 139 assert.ok(start, "start listener must be called already");
michael@0 140 assert.equal(false, stop, "stop callback must not be called yet");
michael@0 141 }
michael@0 142
michael@0 143 function finish() {
michael@0 144 assert.ok(pbUtils.getMode(), true,
michael@0 145 "private mode is active when start event is emitted");
michael@0 146 assert.ok(pb.isActive,
michael@0 147 "`isActive` is `true` when start event is emitted");
michael@0 148 assert.ok(pb.isPrivate(),
michael@0 149 "`isPrivate()` is `true` when start event is emitted");
michael@0 150
michael@0 151 pb.removeListener("start", finish);
michael@0 152 pb.removeListener("stop", onStop);
michael@0 153
michael@0 154 pb.deactivate();
michael@0 155 pb.once("stop", function () {
michael@0 156 assert.equal(pbUtils.getMode(), false);
michael@0 157 assert.equal(pb.isActive, false);
michael@0 158 assert.equal(pb.isPrivate(), false);
michael@0 159
michael@0 160 done();
michael@0 161 });
michael@0 162 }
michael@0 163
michael@0 164 pb.on("start", onStart);
michael@0 165 pb.on("start", onStart2);
michael@0 166 pb.activate();
michael@0 167 };
michael@0 168
michael@0 169 exports.testAutomaticUnload = function(assert, done) {
michael@0 170 setPref(DEPRECATE_PREF, true);
michael@0 171
michael@0 172 // Create another private browsing instance and unload it
michael@0 173 let { loader, errors } = LoaderWithHookedConsole(module);
michael@0 174 let pb2 = loader.require("sdk/private-browsing");
michael@0 175 let called = false;
michael@0 176 pb2.on("start", function onStart() {
michael@0 177 called = true;
michael@0 178 assert.fail("should not be called:x");
michael@0 179 });
michael@0 180 loader.unload();
michael@0 181
michael@0 182 // Then switch to private mode in order to check that the previous instance
michael@0 183 // is correctly destroyed
michael@0 184 pb.once("start", function onStart() {
michael@0 185 timer.setTimeout(function () {
michael@0 186 assert.ok(!called,
michael@0 187 "First private browsing instance is destroyed and inactive");
michael@0 188 // Must reset to normal mode, so that next test starts with it.
michael@0 189 deactivate(function() {
michael@0 190 assert.ok(errors.length, 0, "should have been 1 deprecation error");
michael@0 191 done();
michael@0 192 });
michael@0 193 }, 0);
michael@0 194 });
michael@0 195
michael@0 196 pb.activate();
michael@0 197 };
michael@0 198
michael@0 199 exports.testUnloadWhileActive = function(assert, done) {
michael@0 200 let called = false;
michael@0 201 let { loader, errors } = LoaderWithHookedConsole(module);
michael@0 202 let pb2 = loader.require("sdk/private-browsing");
michael@0 203 let ul = loader.require("sdk/system/unload");
michael@0 204
michael@0 205 let unloadHappened = false;
michael@0 206 ul.when(function() {
michael@0 207 unloadHappened = true;
michael@0 208 timer.setTimeout(function() {
michael@0 209 pb.deactivate();
michael@0 210 });
michael@0 211 });
michael@0 212 pb2.once("start", function() {
michael@0 213 loader.unload();
michael@0 214 });
michael@0 215 pb2.once("stop", function() {
michael@0 216 called = true;
michael@0 217 assert.ok(unloadHappened, "the unload event should have already occurred.");
michael@0 218 assert.fail("stop should not have been fired");
michael@0 219 });
michael@0 220 pb.once("stop", function() {
michael@0 221 assert.ok(!called, "stop was not called on unload");
michael@0 222 assert.ok(errors.length, 2, "should have been 2 deprecation errors");
michael@0 223 done();
michael@0 224 });
michael@0 225
michael@0 226 pb.activate();
michael@0 227 };
michael@0 228
michael@0 229 exports.testIgnoreWindow = function(assert, done) {
michael@0 230 let window = getMostRecentBrowserWindow();
michael@0 231
michael@0 232 pb.once('start', function() {
michael@0 233 assert.ok(isWindowPrivate(window), 'window is private');
michael@0 234 assert.ok(!pbUtils.ignoreWindow(window), 'window is not ignored');
michael@0 235 pb.once('stop', done);
michael@0 236 pb.deactivate();
michael@0 237 });
michael@0 238 pb.activate();
michael@0 239 };

mercurial