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 { Ci } = require('chrome'); michael@0: const { isPrivateBrowsingSupported } = require('sdk/self'); michael@0: const tabs = require('sdk/tabs'); michael@0: const { browserWindows: windows } = require('sdk/windows'); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { getOwnerWindow } = require('sdk/private-browsing/window/utils'); michael@0: const { is } = require('sdk/system/xul-app'); michael@0: const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils'); michael@0: michael@0: const TAB_URL = 'data:text/html;charset=utf-8,TEST-TAB'; michael@0: michael@0: exports.testIsPrivateBrowsingTrue = function(assert) { michael@0: assert.ok(isPrivateBrowsingSupported, michael@0: 'isPrivateBrowsingSupported property is true'); michael@0: }; michael@0: michael@0: // test tab.open with isPrivate: true michael@0: // test isPrivate on a tab michael@0: // test getOwnerWindow on windows and tabs michael@0: exports.testGetOwnerWindow = function(assert, done) { michael@0: let window = windows.activeWindow; michael@0: let chromeWindow = getOwnerWindow(window); michael@0: assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found'); michael@0: michael@0: tabs.open({ michael@0: url: 'about:blank', michael@0: isPrivate: true, michael@0: onOpen: function(tab) { michael@0: // test that getOwnerWindow works as expected michael@0: if (is('Fennec')) { michael@0: assert.notStrictEqual(chromeWindow, getOwnerWindow(tab)); michael@0: assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow); michael@0: } michael@0: else { michael@0: if (isWindowPBSupported) { michael@0: assert.notStrictEqual(chromeWindow, michael@0: getOwnerWindow(tab), michael@0: 'associated window is not the same for window and window\'s tab'); michael@0: } michael@0: else { michael@0: assert.strictEqual(chromeWindow, michael@0: getOwnerWindow(tab), michael@0: 'associated window is the same for window and window\'s tab'); michael@0: } michael@0: } michael@0: michael@0: let pbSupported = isTabPBSupported || isWindowPBSupported; michael@0: michael@0: // test that the tab is private if it should be michael@0: assert.equal(isPrivate(tab), pbSupported); michael@0: assert.equal(isPrivate(getOwnerWindow(tab)), pbSupported); michael@0: michael@0: tab.close(function() done()); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: // test that it is possible to open a private tab michael@0: exports.testTabOpenPrivate = function(assert, done) { michael@0: tabs.open({ michael@0: url: TAB_URL, michael@0: isPrivate: true, michael@0: onReady: function(tab) { michael@0: assert.equal(tab.url, TAB_URL, 'opened correct tab'); michael@0: assert.equal(isPrivate(tab), (isWindowPBSupported || isTabPBSupported)); michael@0: michael@0: tab.close(function() { michael@0: done(); michael@0: }); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: michael@0: // test that it is possible to open a non private tab michael@0: exports.testTabOpenPrivateDefault = function(assert, done) { michael@0: tabs.open({ michael@0: url: TAB_URL, michael@0: onReady: function(tab) { michael@0: assert.equal(tab.url, TAB_URL, 'opened correct tab'); michael@0: assert.equal(isPrivate(tab), false); michael@0: michael@0: tab.close(function() { michael@0: done(); michael@0: }); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: // test that it is possible to open a non private tab in explicit case michael@0: exports.testTabOpenPrivateOffExplicit = function(assert, done) { michael@0: tabs.open({ michael@0: url: TAB_URL, michael@0: isPrivate: false, michael@0: onReady: function(tab) { michael@0: assert.equal(tab.url, TAB_URL, 'opened correct tab'); michael@0: assert.equal(isPrivate(tab), false); michael@0: michael@0: tab.close(function() { michael@0: done(); michael@0: }); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: // test windows.open with isPrivate: true michael@0: // test isPrivate on a window michael@0: if (!is('Fennec')) { michael@0: // test that it is possible to open a private window michael@0: exports.testWindowOpenPrivate = function(assert, done) { michael@0: windows.open({ michael@0: url: TAB_URL, michael@0: isPrivate: true, michael@0: onOpen: function(window) { michael@0: let tab = window.tabs[0]; michael@0: tab.once('ready', function() { michael@0: assert.equal(tab.url, TAB_URL, 'opened correct tab'); michael@0: assert.equal(isPrivate(tab), isWindowPBSupported, 'tab is private'); michael@0: michael@0: window.close(function() { michael@0: done(); michael@0: }); michael@0: }); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports.testIsPrivateOnWindowOn = function(assert, done) { michael@0: windows.open({ michael@0: isPrivate: true, michael@0: onOpen: function(window) { michael@0: assert.equal(isPrivate(window), isWindowPBSupported, 'isPrivate for a window is true when it should be'); michael@0: assert.equal(isPrivate(window.tabs[0]), isWindowPBSupported, '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.testIsPrivateOnWindowOffImplicit = 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: } michael@0: michael@0: exports.testIsPrivateOnWindowOffExplicit = function(assert, done) { michael@0: windows.open({ michael@0: isPrivate: false, 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: } michael@0: }