addon-sdk/source/test/private-browsing/windows.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/windows.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     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 { pb, pbUtils } = require('./helper');
    1.10 +const { onFocus, openDialog, open } = require('sdk/window/utils');
    1.11 +const { open: openPromise, close, focus, promise } = require('sdk/window/helpers');
    1.12 +const { isPrivate } = require('sdk/private-browsing');
    1.13 +const { browserWindows: windows } = require('sdk/windows');
    1.14 +const { defer } = require('sdk/core/promise');
    1.15 +const tabs = require('sdk/tabs');
    1.16 +
    1.17 +// test openDialog() from window/utils with private option
    1.18 +// test isActive state in pwpb case
    1.19 +// test isPrivate on ChromeWindow
    1.20 +exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
    1.21 +  let win = openDialog({
    1.22 +    private: true
    1.23 +  });
    1.24 +
    1.25 +  promise(win, 'DOMContentLoaded').then(function onload() {
    1.26 +    assert.equal(pbUtils.getMode(win),
    1.27 +                 true, 'Newly opened window is in PB mode');
    1.28 +    assert.ok(isPrivate(win), 'isPrivate(window) is true');
    1.29 +    assert.equal(pb.isActive, false, 'PB mode is not active');
    1.30 +
    1.31 +    close(win).then(function() {
    1.32 +      assert.equal(pb.isActive, false, 'PB mode is not active');
    1.33 +      done();
    1.34 +    });
    1.35 +  });
    1.36 +}
    1.37 +
    1.38 +// test open() from window/utils with private feature
    1.39 +// test isActive state in pwpb case
    1.40 +// test isPrivate on ChromeWindow
    1.41 +exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
    1.42 +  let win = open('chrome://browser/content/browser.xul', {
    1.43 +    features: {
    1.44 +      private: true
    1.45 +    }
    1.46 +  });
    1.47 +
    1.48 +  promise(win, 'DOMContentLoaded').then(function onload() {
    1.49 +    assert.equal(pbUtils.getMode(win),
    1.50 +                 true, 'Newly opened window is in PB mode');
    1.51 +    assert.ok(isPrivate(win), 'isPrivate(window) is true');
    1.52 +    assert.equal(pb.isActive, false, 'PB mode is not active');
    1.53 +
    1.54 +    close(win).then(function() {
    1.55 +      assert.equal(pb.isActive, false, 'PB mode is not active');
    1.56 +      done();
    1.57 +    });
    1.58 +  });
    1.59 +}
    1.60 +
    1.61 +exports.testIsPrivateOnWindowOpen = function(assert, done) {
    1.62 +  windows.open({
    1.63 +    isPrivate: true,
    1.64 +    onOpen: function(window) {
    1.65 +      assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be');
    1.66 +      assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
    1.67 +      window.close(done);
    1.68 +    }
    1.69 +  });
    1.70 +}
    1.71 +
    1.72 +exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) {
    1.73 +    // open a private window
    1.74 +    openPromise(null, {
    1.75 +      features: {
    1.76 +        private: true,
    1.77 +        chrome: true,
    1.78 +        titlebar: true,
    1.79 +        toolbar: true
    1.80 +      }
    1.81 +    }).then(focus).then(function(window) {
    1.82 +      let { promise, resolve } = defer();
    1.83 +
    1.84 +      assert.equal(isPrivate(window), true, 'the only open window is private');
    1.85 +
    1.86 +      windows.open({
    1.87 +        url: 'about:blank',
    1.88 +        onOpen: function(w) {
    1.89 +          assert.equal(isPrivate(w), false, 'new test window is not private');
    1.90 +          w.close(function() resolve(window));
    1.91 +        }
    1.92 +      });
    1.93 +
    1.94 +      return promise;
    1.95 +    }).then(close).
    1.96 +       then(done, assert.fail);
    1.97 +};
    1.98 +
    1.99 +exports.testOpenTabWithPrivateWindow = function(assert, done) {
   1.100 +  function start() {
   1.101 +    openPromise(null, {
   1.102 +      features: {
   1.103 +        private: true,
   1.104 +        toolbar: true
   1.105 +      }
   1.106 +    }).then(focus).then(function(window) {
   1.107 +      let { promise, resolve } = defer();
   1.108 +      assert.equal(isPrivate(window), true, 'the focused window is private');
   1.109 +
   1.110 +      tabs.open({
   1.111 +        url: 'about:blank',
   1.112 +        onOpen: function(tab) {
   1.113 +          assert.equal(isPrivate(tab), false, 'the opened tab is not private');
   1.114 +          // not closing this tab on purpose.. for now...
   1.115 +          // we keep this tab open because we closed all windows
   1.116 +          // and must keep a non-private window open at end of this test for next ones.
   1.117 +          resolve(window);
   1.118 +        }
   1.119 +      });
   1.120 +
   1.121 +      return promise;
   1.122 +    }).then(close).then(done, assert.fail);
   1.123 +  }
   1.124 +
   1.125 +  (function closeWindows() {
   1.126 +    if (windows.length > 0) {
   1.127 +      return windows.activeWindow.close(closeWindows);
   1.128 +    }
   1.129 +    assert.pass('all pre test windows have been closed');
   1.130 +    return start();
   1.131 +  })()
   1.132 +};
   1.133 +
   1.134 +exports.testIsPrivateOnWindowOff = function(assert, done) {
   1.135 +  windows.open({
   1.136 +    onOpen: function(window) {
   1.137 +      assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
   1.138 +      assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
   1.139 +      window.close(done);
   1.140 +    }
   1.141 +  })
   1.142 +}

mercurial