michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const timer = require("sdk/timers"); michael@0: const { LoaderWithHookedConsole, deactivate, pb, pbUtils } = require("./helper"); michael@0: const tabs = require("sdk/tabs"); michael@0: const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils'); michael@0: const { set: setPref } = require("sdk/preferences/service"); michael@0: const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings"; michael@0: michael@0: exports["test activate private mode via handler"] = function(assert, done) { michael@0: function onReady(tab) { michael@0: if (tab.url == "about:robots") michael@0: tab.close(function() pb.activate()); michael@0: } michael@0: function cleanup(tab) { michael@0: if (tab.url == "about:") { michael@0: tabs.removeListener("ready", cleanup); michael@0: tab.close(function onClose() { michael@0: done(); michael@0: }); michael@0: } michael@0: } michael@0: michael@0: tabs.on("ready", onReady); michael@0: pb.once("start", function onStart() { michael@0: assert.pass("private mode was activated"); michael@0: pb.deactivate(); michael@0: }); michael@0: pb.once("stop", function onStop() { michael@0: assert.pass("private mode was deactivated"); michael@0: tabs.removeListener("ready", onReady); michael@0: tabs.on("ready", cleanup); michael@0: }); michael@0: tabs.once("open", function onOpen() { michael@0: tabs.open("about:robots"); michael@0: }); michael@0: tabs.open("about:"); michael@0: }; michael@0: michael@0: // tests that isActive has the same value as the private browsing service michael@0: // expects michael@0: exports.testGetIsActive = function (assert) { michael@0: assert.equal(pb.isActive, false, michael@0: "private-browsing.isActive is correct without modifying PB service"); michael@0: assert.equal(pb.isPrivate(), false, michael@0: "private-browsing.sPrivate() is correct without modifying PB service"); michael@0: michael@0: pb.once("start", function() { michael@0: assert.ok(pb.isActive, michael@0: "private-browsing.isActive is correct after modifying PB service"); michael@0: assert.ok(pb.isPrivate(), michael@0: "private-browsing.sPrivate() is correct after modifying PB service"); michael@0: // Switch back to normal mode. michael@0: pb.deactivate(); michael@0: }); michael@0: pb.activate(); michael@0: michael@0: pb.once("stop", function() { michael@0: assert.ok(!pb.isActive, michael@0: "private-browsing.isActive is correct after modifying PB service"); michael@0: assert.ok(!pb.isPrivate(), michael@0: "private-browsing.sPrivate() is correct after modifying PB service"); michael@0: test.done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testStart = function(assert, done) { michael@0: pb.on("start", function onStart() { michael@0: assert.equal(this, pb, "`this` should be private-browsing module"); michael@0: assert.ok(pbUtils.getMode(), michael@0: 'private mode is active when "start" event is emitted'); michael@0: assert.ok(pb.isActive, michael@0: '`isActive` is `true` when "start" event is emitted'); michael@0: assert.ok(pb.isPrivate(), michael@0: '`isPrivate` is `true` when "start" event is emitted'); michael@0: pb.removeListener("start", onStart); michael@0: deactivate(done); michael@0: }); michael@0: pb.activate(); michael@0: }; michael@0: michael@0: exports.testStop = function(assert, done) { michael@0: pb.once("stop", function onStop() { michael@0: assert.equal(this, pb, "`this` should be private-browsing module"); michael@0: assert.equal(pbUtils.getMode(), false, michael@0: "private mode is disabled when stop event is emitted"); michael@0: assert.equal(pb.isActive, false, michael@0: "`isActive` is `false` when stop event is emitted"); michael@0: assert.equal(pb.isPrivate(), false, michael@0: "`isPrivate()` is `false` when stop event is emitted"); michael@0: done(); michael@0: }); michael@0: pb.activate(); michael@0: pb.once("start", function() { michael@0: pb.deactivate(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testBothListeners = function(assert, done) { michael@0: let stop = false; michael@0: let start = false; michael@0: michael@0: function onStop() { michael@0: assert.equal(stop, false, michael@0: "stop callback must be called only once"); michael@0: assert.equal(pbUtils.getMode(), false, michael@0: "private mode is disabled when stop event is emitted"); michael@0: assert.equal(pb.isActive, false, michael@0: "`isActive` is `false` when stop event is emitted"); michael@0: assert.equal(pb.isPrivate(), false, michael@0: "`isPrivate()` is `false` when stop event is emitted"); michael@0: michael@0: pb.on("start", finish); michael@0: pb.removeListener("start", onStart); michael@0: pb.removeListener("start", onStart2); michael@0: pb.activate(); michael@0: stop = true; michael@0: } michael@0: michael@0: function onStart() { michael@0: assert.equal(false, start, michael@0: "stop callback must be called only once"); michael@0: assert.ok(pbUtils.getMode(), michael@0: "private mode is active when start event is emitted"); michael@0: assert.ok(pb.isActive, michael@0: "`isActive` is `true` when start event is emitted"); michael@0: assert.ok(pb.isPrivate(), michael@0: "`isPrivate()` is `true` when start event is emitted"); michael@0: michael@0: pb.on("stop", onStop); michael@0: pb.deactivate(); michael@0: start = true; michael@0: } michael@0: michael@0: function onStart2() { michael@0: assert.ok(start, "start listener must be called already"); michael@0: assert.equal(false, stop, "stop callback must not be called yet"); michael@0: } michael@0: michael@0: function finish() { michael@0: assert.ok(pbUtils.getMode(), true, michael@0: "private mode is active when start event is emitted"); michael@0: assert.ok(pb.isActive, michael@0: "`isActive` is `true` when start event is emitted"); michael@0: assert.ok(pb.isPrivate(), michael@0: "`isPrivate()` is `true` when start event is emitted"); michael@0: michael@0: pb.removeListener("start", finish); michael@0: pb.removeListener("stop", onStop); michael@0: michael@0: pb.deactivate(); michael@0: pb.once("stop", function () { michael@0: assert.equal(pbUtils.getMode(), false); michael@0: assert.equal(pb.isActive, false); michael@0: assert.equal(pb.isPrivate(), false); michael@0: michael@0: done(); michael@0: }); michael@0: } michael@0: michael@0: pb.on("start", onStart); michael@0: pb.on("start", onStart2); michael@0: pb.activate(); michael@0: }; michael@0: michael@0: exports.testAutomaticUnload = function(assert, done) { michael@0: setPref(DEPRECATE_PREF, true); michael@0: michael@0: // Create another private browsing instance and unload it michael@0: let { loader, errors } = LoaderWithHookedConsole(module); michael@0: let pb2 = loader.require("sdk/private-browsing"); michael@0: let called = false; michael@0: pb2.on("start", function onStart() { michael@0: called = true; michael@0: assert.fail("should not be called:x"); michael@0: }); michael@0: loader.unload(); michael@0: michael@0: // Then switch to private mode in order to check that the previous instance michael@0: // is correctly destroyed michael@0: pb.once("start", function onStart() { michael@0: timer.setTimeout(function () { michael@0: assert.ok(!called, michael@0: "First private browsing instance is destroyed and inactive"); michael@0: // Must reset to normal mode, so that next test starts with it. michael@0: deactivate(function() { michael@0: assert.ok(errors.length, 0, "should have been 1 deprecation error"); michael@0: done(); michael@0: }); michael@0: }, 0); michael@0: }); michael@0: michael@0: pb.activate(); michael@0: }; michael@0: michael@0: exports.testUnloadWhileActive = function(assert, done) { michael@0: let called = false; michael@0: let { loader, errors } = LoaderWithHookedConsole(module); michael@0: let pb2 = loader.require("sdk/private-browsing"); michael@0: let ul = loader.require("sdk/system/unload"); michael@0: michael@0: let unloadHappened = false; michael@0: ul.when(function() { michael@0: unloadHappened = true; michael@0: timer.setTimeout(function() { michael@0: pb.deactivate(); michael@0: }); michael@0: }); michael@0: pb2.once("start", function() { michael@0: loader.unload(); michael@0: }); michael@0: pb2.once("stop", function() { michael@0: called = true; michael@0: assert.ok(unloadHappened, "the unload event should have already occurred."); michael@0: assert.fail("stop should not have been fired"); michael@0: }); michael@0: pb.once("stop", function() { michael@0: assert.ok(!called, "stop was not called on unload"); michael@0: assert.ok(errors.length, 2, "should have been 2 deprecation errors"); michael@0: done(); michael@0: }); michael@0: michael@0: pb.activate(); michael@0: }; michael@0: michael@0: exports.testIgnoreWindow = function(assert, done) { michael@0: let window = getMostRecentBrowserWindow(); michael@0: michael@0: pb.once('start', function() { michael@0: assert.ok(isWindowPrivate(window), 'window is private'); michael@0: assert.ok(!pbUtils.ignoreWindow(window), 'window is not ignored'); michael@0: pb.once('stop', done); michael@0: pb.deactivate(); michael@0: }); michael@0: pb.activate(); michael@0: };