michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { pb, pbUtils } = require('./helper'); michael@0: const { onFocus, openDialog, open } = require('sdk/window/utils'); michael@0: const { open: openPromise, close, focus, promise } = require('sdk/window/helpers'); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { browserWindows: windows } = require('sdk/windows'); michael@0: const { defer } = require('sdk/core/promise'); michael@0: const tabs = require('sdk/tabs'); michael@0: michael@0: // test openDialog() from window/utils with private option michael@0: // test isActive state in pwpb case michael@0: // test isPrivate on ChromeWindow michael@0: exports.testPerWindowPrivateBrowsingGetter = function(assert, done) { michael@0: let win = openDialog({ michael@0: private: true michael@0: }); michael@0: michael@0: promise(win, 'DOMContentLoaded').then(function onload() { michael@0: assert.equal(pbUtils.getMode(win), michael@0: true, 'Newly opened window is in PB mode'); michael@0: assert.ok(isPrivate(win), 'isPrivate(window) is true'); michael@0: assert.equal(pb.isActive, false, 'PB mode is not active'); michael@0: michael@0: close(win).then(function() { michael@0: assert.equal(pb.isActive, false, 'PB mode is not active'); michael@0: done(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: // test open() from window/utils with private feature michael@0: // test isActive state in pwpb case michael@0: // test isPrivate on ChromeWindow michael@0: exports.testPerWindowPrivateBrowsingGetter = function(assert, done) { michael@0: let win = open('chrome://browser/content/browser.xul', { michael@0: features: { michael@0: private: true michael@0: } michael@0: }); michael@0: michael@0: promise(win, 'DOMContentLoaded').then(function onload() { michael@0: assert.equal(pbUtils.getMode(win), michael@0: true, 'Newly opened window is in PB mode'); michael@0: assert.ok(isPrivate(win), 'isPrivate(window) is true'); michael@0: assert.equal(pb.isActive, false, 'PB mode is not active'); michael@0: michael@0: close(win).then(function() { michael@0: assert.equal(pb.isActive, false, 'PB mode is not active'); michael@0: done(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: exports.testIsPrivateOnWindowOpen = function(assert, done) { michael@0: windows.open({ michael@0: isPrivate: true, michael@0: onOpen: function(window) { michael@0: assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be'); michael@0: assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); michael@0: window.close(done); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) { michael@0: // open a private window michael@0: openPromise(null, { michael@0: features: { michael@0: private: true, michael@0: chrome: true, michael@0: titlebar: true, michael@0: toolbar: true michael@0: } michael@0: }).then(focus).then(function(window) { michael@0: let { promise, resolve } = defer(); michael@0: michael@0: assert.equal(isPrivate(window), true, 'the only open window is private'); michael@0: michael@0: windows.open({ michael@0: url: 'about:blank', michael@0: onOpen: function(w) { michael@0: assert.equal(isPrivate(w), false, 'new test window is not private'); michael@0: w.close(function() resolve(window)); michael@0: } michael@0: }); michael@0: michael@0: return promise; michael@0: }).then(close). michael@0: then(done, assert.fail); michael@0: }; michael@0: michael@0: exports.testOpenTabWithPrivateWindow = function(assert, done) { michael@0: function start() { michael@0: openPromise(null, { michael@0: features: { michael@0: private: true, michael@0: toolbar: true michael@0: } michael@0: }).then(focus).then(function(window) { michael@0: let { promise, resolve } = defer(); michael@0: assert.equal(isPrivate(window), true, 'the focused window is private'); michael@0: michael@0: tabs.open({ michael@0: url: 'about:blank', michael@0: onOpen: function(tab) { michael@0: assert.equal(isPrivate(tab), false, 'the opened tab is not private'); michael@0: // not closing this tab on purpose.. for now... michael@0: // we keep this tab open because we closed all windows michael@0: // and must keep a non-private window open at end of this test for next ones. michael@0: resolve(window); michael@0: } michael@0: }); michael@0: michael@0: return promise; michael@0: }).then(close).then(done, assert.fail); michael@0: } michael@0: michael@0: (function closeWindows() { michael@0: if (windows.length > 0) { michael@0: return windows.activeWindow.close(closeWindows); michael@0: } michael@0: assert.pass('all pre test windows have been closed'); michael@0: return start(); michael@0: })() michael@0: }; michael@0: michael@0: exports.testIsPrivateOnWindowOff = function(assert, done) { michael@0: windows.open({ michael@0: onOpen: function(window) { michael@0: assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be'); michael@0: assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); michael@0: window.close(done); michael@0: } michael@0: }) michael@0: }