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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/private-browsing/global.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,239 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +const timer = require("sdk/timers");
    1.10 +const { LoaderWithHookedConsole, deactivate, pb, pbUtils } = require("./helper");
    1.11 +const tabs = require("sdk/tabs");
    1.12 +const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
    1.13 +const { set: setPref } = require("sdk/preferences/service");
    1.14 +const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
    1.15 +
    1.16 +exports["test activate private mode via handler"] = function(assert, done) {
    1.17 +  function onReady(tab) {
    1.18 +    if (tab.url == "about:robots")
    1.19 +      tab.close(function() pb.activate());
    1.20 +  }
    1.21 +  function cleanup(tab) {
    1.22 +    if (tab.url == "about:") {
    1.23 +      tabs.removeListener("ready", cleanup);
    1.24 +      tab.close(function onClose() {
    1.25 +        done();
    1.26 +      });
    1.27 +    }
    1.28 +  }
    1.29 +
    1.30 +  tabs.on("ready", onReady);
    1.31 +  pb.once("start", function onStart() {
    1.32 +    assert.pass("private mode was activated");
    1.33 +    pb.deactivate();
    1.34 +  });
    1.35 +  pb.once("stop", function onStop() {
    1.36 +    assert.pass("private mode was deactivated");
    1.37 +    tabs.removeListener("ready", onReady);
    1.38 +    tabs.on("ready", cleanup);
    1.39 +  });
    1.40 +  tabs.once("open", function onOpen() {
    1.41 +    tabs.open("about:robots");
    1.42 +  });
    1.43 +  tabs.open("about:");
    1.44 +};
    1.45 +
    1.46 +// tests that isActive has the same value as the private browsing service
    1.47 +// expects
    1.48 +exports.testGetIsActive = function (assert) {
    1.49 +  assert.equal(pb.isActive, false,
    1.50 +                   "private-browsing.isActive is correct without modifying PB service");
    1.51 +  assert.equal(pb.isPrivate(), false,
    1.52 +                   "private-browsing.sPrivate() is correct without modifying PB service");
    1.53 +
    1.54 +  pb.once("start", function() {
    1.55 +    assert.ok(pb.isActive,
    1.56 +                  "private-browsing.isActive is correct after modifying PB service");
    1.57 +    assert.ok(pb.isPrivate(),
    1.58 +                  "private-browsing.sPrivate() is correct after modifying PB service");
    1.59 +    // Switch back to normal mode.
    1.60 +    pb.deactivate();
    1.61 +  });
    1.62 +  pb.activate();
    1.63 +
    1.64 +  pb.once("stop", function() {
    1.65 +    assert.ok(!pb.isActive,
    1.66 +                "private-browsing.isActive is correct after modifying PB service");
    1.67 +    assert.ok(!pb.isPrivate(),
    1.68 +                "private-browsing.sPrivate() is correct after modifying PB service");
    1.69 +    test.done();
    1.70 +  });
    1.71 +};
    1.72 +
    1.73 +exports.testStart = function(assert, done) {
    1.74 +  pb.on("start", function onStart() {
    1.75 +    assert.equal(this, pb, "`this` should be private-browsing module");
    1.76 +    assert.ok(pbUtils.getMode(),
    1.77 +                'private mode is active when "start" event is emitted');
    1.78 +    assert.ok(pb.isActive,
    1.79 +                '`isActive` is `true` when "start" event is emitted');
    1.80 +    assert.ok(pb.isPrivate(),
    1.81 +                '`isPrivate` is `true` when "start" event is emitted');
    1.82 +    pb.removeListener("start", onStart);
    1.83 +    deactivate(done);
    1.84 +  });
    1.85 +  pb.activate();
    1.86 +};
    1.87 +
    1.88 +exports.testStop = function(assert, done) {
    1.89 +  pb.once("stop", function onStop() {
    1.90 +    assert.equal(this, pb, "`this` should be private-browsing module");
    1.91 +    assert.equal(pbUtils.getMode(), false,
    1.92 +                     "private mode is disabled when stop event is emitted");
    1.93 +    assert.equal(pb.isActive, false,
    1.94 +                     "`isActive` is `false` when stop event is emitted");
    1.95 +    assert.equal(pb.isPrivate(), false,
    1.96 +                     "`isPrivate()` is `false` when stop event is emitted");
    1.97 +    done();
    1.98 +  });
    1.99 +  pb.activate();
   1.100 +  pb.once("start", function() {
   1.101 +    pb.deactivate();
   1.102 +  });
   1.103 +};
   1.104 +
   1.105 +exports.testBothListeners = function(assert, done) {
   1.106 +  let stop = false;
   1.107 +  let start = false;
   1.108 +
   1.109 +  function onStop() {
   1.110 +    assert.equal(stop, false,
   1.111 +                     "stop callback must be called only once");
   1.112 +    assert.equal(pbUtils.getMode(), false,
   1.113 +                     "private mode is disabled when stop event is emitted");
   1.114 +    assert.equal(pb.isActive, false,
   1.115 +                     "`isActive` is `false` when stop event is emitted");
   1.116 +    assert.equal(pb.isPrivate(), false,
   1.117 +                     "`isPrivate()` is `false` when stop event is emitted");
   1.118 +
   1.119 +    pb.on("start", finish);
   1.120 +    pb.removeListener("start", onStart);
   1.121 +    pb.removeListener("start", onStart2);
   1.122 +    pb.activate();
   1.123 +    stop = true;
   1.124 +  }
   1.125 +
   1.126 +  function onStart() {
   1.127 +    assert.equal(false, start,
   1.128 +                     "stop callback must be called only once");
   1.129 +    assert.ok(pbUtils.getMode(),
   1.130 +                "private mode is active when start event is emitted");
   1.131 +    assert.ok(pb.isActive,
   1.132 +                "`isActive` is `true` when start event is emitted");
   1.133 +    assert.ok(pb.isPrivate(),
   1.134 +                "`isPrivate()` is `true` when start event is emitted");
   1.135 +
   1.136 +    pb.on("stop", onStop);
   1.137 +    pb.deactivate();
   1.138 +    start = true;
   1.139 +  }
   1.140 +
   1.141 +  function onStart2() {
   1.142 +    assert.ok(start, "start listener must be called already");
   1.143 +    assert.equal(false, stop, "stop callback must not be called yet");
   1.144 +  }
   1.145 +
   1.146 +  function finish() {
   1.147 +    assert.ok(pbUtils.getMode(), true,
   1.148 +                "private mode is active when start event is emitted");
   1.149 +    assert.ok(pb.isActive,
   1.150 +                "`isActive` is `true` when start event is emitted");
   1.151 +    assert.ok(pb.isPrivate(),
   1.152 +                "`isPrivate()` is `true` when start event is emitted");
   1.153 +
   1.154 +    pb.removeListener("start", finish);
   1.155 +    pb.removeListener("stop", onStop);
   1.156 +
   1.157 +    pb.deactivate();
   1.158 +    pb.once("stop", function () {
   1.159 +      assert.equal(pbUtils.getMode(), false);
   1.160 +      assert.equal(pb.isActive, false);
   1.161 +      assert.equal(pb.isPrivate(), false);
   1.162 +
   1.163 +      done();
   1.164 +    });
   1.165 +  }
   1.166 +
   1.167 +  pb.on("start", onStart);
   1.168 +  pb.on("start", onStart2);
   1.169 +  pb.activate();
   1.170 +};
   1.171 +
   1.172 +exports.testAutomaticUnload = function(assert, done) {
   1.173 +  setPref(DEPRECATE_PREF, true);
   1.174 +
   1.175 +  // Create another private browsing instance and unload it
   1.176 +  let { loader, errors } = LoaderWithHookedConsole(module);
   1.177 +  let pb2 = loader.require("sdk/private-browsing");
   1.178 +  let called = false;
   1.179 +  pb2.on("start", function onStart() {
   1.180 +    called = true;
   1.181 +    assert.fail("should not be called:x");
   1.182 +  });
   1.183 +  loader.unload();
   1.184 +
   1.185 +  // Then switch to private mode in order to check that the previous instance
   1.186 +  // is correctly destroyed
   1.187 +  pb.once("start", function onStart() {
   1.188 +    timer.setTimeout(function () {
   1.189 +      assert.ok(!called, 
   1.190 +        "First private browsing instance is destroyed and inactive");
   1.191 +      // Must reset to normal mode, so that next test starts with it.
   1.192 +      deactivate(function() {
   1.193 +        assert.ok(errors.length, 0, "should have been 1 deprecation error");
   1.194 +        done();
   1.195 +      });
   1.196 +    }, 0);
   1.197 +  });
   1.198 +
   1.199 +  pb.activate();
   1.200 +};
   1.201 +
   1.202 +exports.testUnloadWhileActive = function(assert, done) {
   1.203 +  let called = false;
   1.204 +  let { loader, errors } = LoaderWithHookedConsole(module);
   1.205 +  let pb2 = loader.require("sdk/private-browsing");
   1.206 +  let ul = loader.require("sdk/system/unload");
   1.207 +
   1.208 +  let unloadHappened = false;
   1.209 +  ul.when(function() {
   1.210 +    unloadHappened = true;
   1.211 +    timer.setTimeout(function() {
   1.212 +      pb.deactivate();
   1.213 +    });
   1.214 +  });
   1.215 +  pb2.once("start", function() {
   1.216 +    loader.unload();
   1.217 +  });
   1.218 +  pb2.once("stop", function() {
   1.219 +    called = true;
   1.220 +    assert.ok(unloadHappened, "the unload event should have already occurred.");
   1.221 +    assert.fail("stop should not have been fired");
   1.222 +  });
   1.223 +  pb.once("stop", function() {
   1.224 +    assert.ok(!called, "stop was not called on unload");
   1.225 +    assert.ok(errors.length, 2, "should have been 2 deprecation errors");
   1.226 +    done();
   1.227 +  });
   1.228 +
   1.229 +  pb.activate();
   1.230 +};
   1.231 +
   1.232 +exports.testIgnoreWindow = function(assert, done) {
   1.233 +  let window = getMostRecentBrowserWindow();
   1.234 +
   1.235 +  pb.once('start', function() {
   1.236 +    assert.ok(isWindowPrivate(window), 'window is private');
   1.237 +    assert.ok(!pbUtils.ignoreWindow(window), 'window is not ignored');
   1.238 +    pb.once('stop', done);
   1.239 +    pb.deactivate();
   1.240 +  });
   1.241 +  pb.activate();
   1.242 +};

mercurial