addon-sdk/source/test/addons/private-browsing-supported/test-windows.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.

     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 { Cc, Ci } = require('chrome');
     7 const { isPrivate } = require('sdk/private-browsing');
     8 const { isWindowPBSupported } = require('sdk/private-browsing/utils');
     9 const { onFocus, getMostRecentWindow, getWindowTitle, getInnerId,
    10         getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils');
    11 const { open, close, focus, promise } = require('sdk/window/helpers');
    12 const { browserWindows } = require("sdk/windows");
    13 const winUtils = require("sdk/deprecated/window-utils");
    14 const { fromIterator: toArray } = require('sdk/util/array');
    15 const tabs = require('sdk/tabs');
    17 const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
    19 const BROWSER = 'chrome://browser/content/browser.xul';
    21 function makeEmptyBrowserWindow(options) {
    22   options = options || {};
    23   return open(BROWSER, {
    24     features: {
    25       chrome: true,
    26       private: !!options.private
    27     }
    28   }).then(focus);
    29 }
    31 exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
    32   var myNonPrivateWindowId, myPrivateWindowId;
    33   var privateWindowClosed = false;
    34   var privateWindowOpened = false;
    35   var trackedWindowIds = [];
    37   let wt = winUtils.WindowTracker({
    38     onTrack: function(window) {
    39       let id = getInnerId(window);
    40       trackedWindowIds.push(id);
    41     },
    42     onUntrack: function(window) {
    43       let id = getInnerId(window);
    44       if (id === myPrivateWindowId) {
    45         privateWindowClosed = true;
    46       }
    48       if (id === myNonPrivateWindowId) {
    49         assert.equal(privateWindowClosed, true, 'private window was untracked');
    50         wt.unload();
    51         done();
    52       }
    53     }
    54   });
    56   // make a new private window
    57   makeEmptyBrowserWindow({ private: true }).then(function(window) {
    58     myPrivateWindowId = getInnerId(window);
    60     assert.ok(trackedWindowIds.indexOf(myPrivateWindowId) >= 0, 'private window was tracked');
    61     assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate');
    62     assert.equal(isWindowPrivate(window), isWindowPBSupported);
    63     assert.ok(getFrames(window).length > 1, 'there are frames for private window');
    64     assert.equal(getWindowTitle(window), window.document.title,
    65                  'getWindowTitle works');
    67     return close(window).then(function() {
    68       assert.pass('private window was closed');
    70       return makeEmptyBrowserWindow().then(function(window) {
    71         myNonPrivateWindowId = getInnerId(window);
    72         assert.notEqual(myPrivateWindowId, myNonPrivateWindowId, 'non private window was opened');
    73         return close(window);
    74       });
    75     });
    76   }).then(null, assert.fail);
    77 };
    79 // Test setting activeWIndow and onFocus for private windows
    80 exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
    81   let browserWindow = WM.getMostRecentWindow("navigator:browser");
    82   let testSteps;
    84   assert.equal(winUtils.activeBrowserWindow, browserWindow,
    85                "Browser window is the active browser window.");
    86   assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
    88   // make a new private window
    89   makeEmptyBrowserWindow({
    90     private: true
    91   }).then(function(window) {
    92     let continueAfterFocus = function(window) onFocus(window).then(nextTest);
    94     // PWPB case
    95     if (isWindowPBSupported) {
    96       assert.ok(isPrivate(window), "window is private");
    97       assert.notDeepEqual(winUtils.activeBrowserWindow, browserWindow);
    98     }
    99     // Global case
   100     else {
   101       assert.ok(!isPrivate(window), "window is not private");
   102     }
   104     assert.strictEqual(winUtils.activeBrowserWindow, window,
   105                  "Correct active browser window pb supported");
   106     assert.notStrictEqual(browserWindow, window,
   107                  "The window is not the old browser window");
   109     testSteps = [
   110       function() {
   111         // test setting a non private window
   112         continueAfterFocus(winUtils.activeWindow = browserWindow);
   113       },
   114       function() {
   115         assert.strictEqual(winUtils.activeWindow, browserWindow,
   116                            "Correct active window [1]");
   117         assert.strictEqual(winUtils.activeBrowserWindow, browserWindow,
   118                            "Correct active browser window [1]");
   120         // test focus(window)
   121         focus(window).then(nextTest);
   122       },
   123       function(w) {
   124         assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
   125         assert.strictEqual(winUtils.activeBrowserWindow, window,
   126                            "Correct active browser window [2]");
   127         assert.strictEqual(winUtils.activeWindow, window,
   128                            "Correct active window [2]");
   130         // test setting a private window
   131         continueAfterFocus(winUtils.activeWindow = window);
   132       },
   133       function() {
   134         assert.deepEqual(winUtils.activeBrowserWindow, window,
   135                          "Correct active browser window [3]");
   136         assert.deepEqual(winUtils.activeWindow, window,
   137                          "Correct active window [3]");
   139         // just to get back to original state
   140         continueAfterFocus(winUtils.activeWindow = browserWindow);
   141       },
   142       function() {
   143         assert.deepEqual(winUtils.activeBrowserWindow, browserWindow,
   144                          "Correct active browser window when pb mode is supported [4]");
   145         assert.deepEqual(winUtils.activeWindow, browserWindow,
   146                          "Correct active window when pb mode is supported [4]");
   148         close(window).then(done).then(null, assert.fail);
   149       }
   150     ];
   152     function nextTest() {
   153       let args = arguments;
   154       if (testSteps.length) {
   155         require('sdk/timers').setTimeout(function() {
   156           (testSteps.shift()).apply(null, args);
   157         }, 0);
   158       }
   159     }
   160     nextTest();
   161   });
   162 };
   164 exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
   165   // make a new private window
   166   makeEmptyBrowserWindow({
   167     private: true
   168   }).then(function(window) {
   169     // PWPB case
   170     if (isWindowPBSupported) {
   171       assert.equal(isPrivate(winUtils.activeWindow), true,
   172                    "active window is private");
   173       assert.equal(isPrivate(winUtils.activeBrowserWindow), true,
   174                    "active browser window is private");
   175       assert.ok(isWindowPrivate(window), "window is private");
   176       assert.ok(isPrivate(window), "window is private");
   178       // pb mode is supported
   179       assert.ok(
   180         isWindowPrivate(winUtils.activeWindow),
   181         "active window is private when pb mode is supported");
   182       assert.ok(
   183         isWindowPrivate(winUtils.activeBrowserWindow),
   184         "active browser window is private when pb mode is supported");
   185       assert.ok(isPrivate(winUtils.activeWindow),
   186                 "active window is private when pb mode is supported");
   187       assert.ok(isPrivate(winUtils.activeBrowserWindow),
   188         "active browser window is private when pb mode is supported");
   189     }
   190     // Global case
   191     else {
   192       assert.equal(isPrivate(winUtils.activeWindow), false,
   193                    "active window is not private");
   194       assert.equal(isPrivate(winUtils.activeBrowserWindow), false,
   195                    "active browser window is not private");
   196       assert.equal(isWindowPrivate(window), false, "window is not private");
   197       assert.equal(isPrivate(window), false, "window is not private");
   198     }
   200     return close(window);
   201   }).then(done).then(null, assert.fail);
   202 }
   204 exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) {
   205   // make a new private window
   206   makeEmptyBrowserWindow({
   207     private: true
   208   }).then(function(window) {
   209     assert.equal(isWindowPrivate(window), isWindowPBSupported);
   210     assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1,
   211               "window is in windowIterator()");
   213     return close(window);
   214   }).then(done).then(null, assert.fail);
   215 };
   217 // test that it is not possible to find a private window in
   218 // windows module's iterator
   219 exports.testWindowIteratorPrivateDefault = function(assert, done) {
   220   // there should only be one window open here, if not give us the
   221   // the urls
   222   if (browserWindows.length > 1) {
   223     for each (let tab in tabs) {
   224       assert.fail("TAB URL: " + tab.url);
   225     }
   226   }
   227   else {
   228     assert.equal(browserWindows.length, 1, 'only one window open');
   229   }
   231   open('chrome://browser/content/browser.xul', {
   232     features: {
   233       private: true,
   234       chrome: true
   235     }
   236   }).then(focus).then(function(window) {
   237     // test that there is a private window opened
   238     assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open');
   239     assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported);
   240     assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported);
   241     assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported);
   243     assert.equal(browserWindows.length, 2, '2 windows open');
   244     assert.equal(windows(null, { includePrivate: true }).length, 2);
   246     return close(window);
   247   }).then(done).then(null, assert.fail);
   248 };

mercurial