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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial