addon-sdk/source/test/test-window-utils-private-browsing.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-window-utils-private-browsing.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     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 +// Fennec support tracked in bug #809412
    1.10 +module.metadata = {
    1.11 +  'engines': {
    1.12 +    'Firefox': '*'
    1.13 +  }
    1.14 +};
    1.15 +
    1.16 +const windowUtils = require('sdk/deprecated/window-utils');
    1.17 +const { Cc, Ci } = require('chrome');
    1.18 +const { isWindowPBSupported } = require('sdk/private-browsing/utils');
    1.19 +const { getFrames, getWindowTitle, onFocus, isWindowPrivate } = require('sdk/window/utils');
    1.20 +const { open, close, focus } = require('sdk/window/helpers');
    1.21 +const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
    1.22 +const { isPrivate } = require('sdk/private-browsing');
    1.23 +const { fromIterator: toArray } = require('sdk/util/array');
    1.24 +const { defer } = require('sdk/core/promise');
    1.25 +const { setTimeout } = require('sdk/timers');
    1.26 +
    1.27 +function tick() {
    1.28 +  let deferred = defer();
    1.29 +  setTimeout(deferred.resolve);
    1.30 +  return deferred.promise;
    1.31 +}
    1.32 +
    1.33 +function makeEmptyBrowserWindow(options) {
    1.34 +  options = options || {};
    1.35 +  return open('chrome://browser/content/browser.xul', {
    1.36 +    features: {
    1.37 +      chrome: true,
    1.38 +      private: !!options.private
    1.39 +    }
    1.40 +  });
    1.41 +}
    1.42 +
    1.43 +exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
    1.44 +  var myNonPrivateWindow, myPrivateWindow;
    1.45 +  var finished = false;
    1.46 +  var privateWindow;
    1.47 +  var privateWindowClosed = false;
    1.48 +
    1.49 +  let wt = windowUtils.WindowTracker({
    1.50 +    onTrack: function(window) {
    1.51 +      assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
    1.52 +    },
    1.53 +    onUntrack: function(window) {
    1.54 +      assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
    1.55 +      // PWPB case
    1.56 +      if (window === myPrivateWindow && isWindowPBSupported) {
    1.57 +        privateWindowClosed = true;
    1.58 +      }
    1.59 +      if (window === myNonPrivateWindow) {
    1.60 +        assert.ok(!privateWindowClosed);
    1.61 +        wt.unload();
    1.62 +        done();
    1.63 +      }
    1.64 +    }
    1.65 +  });
    1.66 +
    1.67 +  // make a new private window
    1.68 +  makeEmptyBrowserWindow({
    1.69 +    private: true
    1.70 +  }).then(function(window) {
    1.71 +    myPrivateWindow = window;
    1.72 +
    1.73 +    assert.equal(isWindowPrivate(window), isWindowPBSupported);
    1.74 +    assert.ok(getFrames(window).length > 1, 'there are frames for private window');
    1.75 +    assert.equal(getWindowTitle(window), window.document.title,
    1.76 +                 'getWindowTitle works');
    1.77 +
    1.78 +    return close(window).then(function() {
    1.79 +      return makeEmptyBrowserWindow().then(function(window) {
    1.80 +        myNonPrivateWindow = window;
    1.81 +        assert.pass('opened new window');
    1.82 +        return close(window);
    1.83 +      });
    1.84 +    });
    1.85 +  }).then(null, assert.fail);
    1.86 +};
    1.87 +
    1.88 +// Test setting activeWIndow and onFocus for private windows
    1.89 +exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
    1.90 +  let browserWindow = WM.getMostRecentWindow("navigator:browser");
    1.91 +
    1.92 +  assert.equal(windowUtils.activeBrowserWindow, browserWindow,
    1.93 +               "Browser window is the active browser window.");
    1.94 +  assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
    1.95 +
    1.96 +  // make a new private window
    1.97 +  makeEmptyBrowserWindow({ private: true }).then(focus).then(window => {
    1.98 +    // PWPB case
    1.99 +    if (isWindowPBSupported) {
   1.100 +      assert.ok(isPrivate(window), "window is private");
   1.101 +      assert.notDeepEqual(windowUtils.activeBrowserWindow, browserWindow);
   1.102 +    }
   1.103 +    // Global case
   1.104 +    else {
   1.105 +      assert.ok(!isPrivate(window), "window is not private");
   1.106 +    }
   1.107 +
   1.108 +    assert.strictEqual(windowUtils.activeBrowserWindow, window,
   1.109 +                 "Correct active browser window pb supported");
   1.110 +    assert.notStrictEqual(browserWindow, window,
   1.111 +                 "The window is not the old browser window");
   1.112 +
   1.113 +
   1.114 +    return onFocus(windowUtils.activeWindow = browserWindow).then(_ => {
   1.115 +      assert.strictEqual(windowUtils.activeWindow, browserWindow,
   1.116 +                         "Correct active window [1]");
   1.117 +      assert.strictEqual(windowUtils.activeBrowserWindow, browserWindow,
   1.118 +                         "Correct active browser window [1]");
   1.119 +
   1.120 +      // test focus(window)
   1.121 +      return focus(window).then(w => {
   1.122 +        assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
   1.123 +      }).then(tick);
   1.124 +    }).then(_ => {
   1.125 +      assert.strictEqual(windowUtils.activeBrowserWindow, window,
   1.126 +                         "Correct active browser window [2]");
   1.127 +      assert.strictEqual(windowUtils.activeWindow, window,
   1.128 +                         "Correct active window [2]");
   1.129 +
   1.130 +      // test setting a private window
   1.131 +      return onFocus(windowUtils.activeWindow = window);
   1.132 +    }).then(function() {
   1.133 +      assert.deepEqual(windowUtils.activeBrowserWindow, window,
   1.134 +                       "Correct active browser window [3]");
   1.135 +      assert.deepEqual(windowUtils.activeWindow, window,
   1.136 +                       "Correct active window [3]");
   1.137 +
   1.138 +      // just to get back to original state
   1.139 +      return onFocus(windowUtils.activeWindow = browserWindow);
   1.140 +    }).then(_ => {
   1.141 +      assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
   1.142 +                       "Correct active browser window when pb mode is supported [4]");
   1.143 +      assert.deepEqual(windowUtils.activeWindow, browserWindow,
   1.144 +                       "Correct active window when pb mode is supported [4]");
   1.145 +
   1.146 +      return close(window);
   1.147 +    })
   1.148 +  }).then(done).then(null, assert.fail);
   1.149 +};
   1.150 +
   1.151 +exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
   1.152 +  // make a new private window
   1.153 +  makeEmptyBrowserWindow({
   1.154 +    private: true
   1.155 +  }).then(function(window) {
   1.156 +    // PWPB case
   1.157 +    if (isWindowPBSupported) {
   1.158 +      assert.equal(isPrivate(windowUtils.activeWindow), true,
   1.159 +                   "active window is private");
   1.160 +      assert.equal(isPrivate(windowUtils.activeBrowserWindow), true,
   1.161 +                   "active browser window is private");
   1.162 +      assert.ok(isWindowPrivate(window), "window is private");
   1.163 +      assert.ok(isPrivate(window), "window is private");
   1.164 +
   1.165 +      // pb mode is supported
   1.166 +      assert.ok(
   1.167 +        isWindowPrivate(windowUtils.activeWindow),
   1.168 +        "active window is private when pb mode is supported");
   1.169 +      assert.ok(
   1.170 +        isWindowPrivate(windowUtils.activeBrowserWindow),
   1.171 +        "active browser window is private when pb mode is supported");
   1.172 +      assert.ok(isPrivate(windowUtils.activeWindow),
   1.173 +                "active window is private when pb mode is supported");
   1.174 +      assert.ok(isPrivate(windowUtils.activeBrowserWindow),
   1.175 +        "active browser window is private when pb mode is supported");
   1.176 +    }
   1.177 +    // Global case
   1.178 +    else {
   1.179 +      assert.equal(isPrivate(windowUtils.activeWindow), false,
   1.180 +                   "active window is not private");
   1.181 +      assert.equal(isPrivate(windowUtils.activeBrowserWindow), false,
   1.182 +                   "active browser window is not private");
   1.183 +      assert.equal(isWindowPrivate(window), false, "window is not private");
   1.184 +      assert.equal(isPrivate(window), false, "window is not private");
   1.185 +    }
   1.186 +
   1.187 +    return close(window);
   1.188 +  }).then(done).then(null, assert.fail);
   1.189 +}
   1.190 +
   1.191 +exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) {
   1.192 +  // make a new private window
   1.193 +  makeEmptyBrowserWindow({
   1.194 +    private: true
   1.195 +  }).then(function(window) {
   1.196 +    // PWPB case
   1.197 +    if (isWindowPBSupported) {
   1.198 +      assert.ok(isWindowPrivate(window), "window is private");
   1.199 +      assert.equal(toArray(windowUtils.windowIterator()).indexOf(window), -1,
   1.200 +                   "window is not in windowIterator()");
   1.201 +    }
   1.202 +    // Global case
   1.203 +    else {
   1.204 +      assert.equal(isWindowPrivate(window), false, "window is not private");
   1.205 +      assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1,
   1.206 +                "window is in windowIterator()");
   1.207 +    }
   1.208 +
   1.209 +    return close(window);
   1.210 +  }).then(done).then(null, assert.fail);
   1.211 +};
   1.212 +
   1.213 +require("sdk/test").run(exports);

mercurial