1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 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 { Ci } = require('chrome'); 1.10 +const { isPrivateBrowsingSupported } = require('sdk/self'); 1.11 +const tabs = require('sdk/tabs'); 1.12 +const { browserWindows: windows } = require('sdk/windows'); 1.13 +const { isPrivate } = require('sdk/private-browsing'); 1.14 +const { getOwnerWindow } = require('sdk/private-browsing/window/utils'); 1.15 +const { is } = require('sdk/system/xul-app'); 1.16 +const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils'); 1.17 + 1.18 +const TAB_URL = 'data:text/html;charset=utf-8,TEST-TAB'; 1.19 + 1.20 +exports.testIsPrivateBrowsingTrue = function(assert) { 1.21 + assert.ok(isPrivateBrowsingSupported, 1.22 + 'isPrivateBrowsingSupported property is true'); 1.23 +}; 1.24 + 1.25 +// test tab.open with isPrivate: true 1.26 +// test isPrivate on a tab 1.27 +// test getOwnerWindow on windows and tabs 1.28 +exports.testGetOwnerWindow = function(assert, done) { 1.29 + let window = windows.activeWindow; 1.30 + let chromeWindow = getOwnerWindow(window); 1.31 + assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found'); 1.32 + 1.33 + tabs.open({ 1.34 + url: 'about:blank', 1.35 + isPrivate: true, 1.36 + onOpen: function(tab) { 1.37 + // test that getOwnerWindow works as expected 1.38 + if (is('Fennec')) { 1.39 + assert.notStrictEqual(chromeWindow, getOwnerWindow(tab)); 1.40 + assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow); 1.41 + } 1.42 + else { 1.43 + if (isWindowPBSupported) { 1.44 + assert.notStrictEqual(chromeWindow, 1.45 + getOwnerWindow(tab), 1.46 + 'associated window is not the same for window and window\'s tab'); 1.47 + } 1.48 + else { 1.49 + assert.strictEqual(chromeWindow, 1.50 + getOwnerWindow(tab), 1.51 + 'associated window is the same for window and window\'s tab'); 1.52 + } 1.53 + } 1.54 + 1.55 + let pbSupported = isTabPBSupported || isWindowPBSupported; 1.56 + 1.57 + // test that the tab is private if it should be 1.58 + assert.equal(isPrivate(tab), pbSupported); 1.59 + assert.equal(isPrivate(getOwnerWindow(tab)), pbSupported); 1.60 + 1.61 + tab.close(function() done()); 1.62 + } 1.63 + }); 1.64 +}; 1.65 + 1.66 +// test that it is possible to open a private tab 1.67 +exports.testTabOpenPrivate = function(assert, done) { 1.68 + tabs.open({ 1.69 + url: TAB_URL, 1.70 + isPrivate: true, 1.71 + onReady: function(tab) { 1.72 + assert.equal(tab.url, TAB_URL, 'opened correct tab'); 1.73 + assert.equal(isPrivate(tab), (isWindowPBSupported || isTabPBSupported)); 1.74 + 1.75 + tab.close(function() { 1.76 + done(); 1.77 + }); 1.78 + } 1.79 + }); 1.80 +} 1.81 + 1.82 + 1.83 +// test that it is possible to open a non private tab 1.84 +exports.testTabOpenPrivateDefault = function(assert, done) { 1.85 + tabs.open({ 1.86 + url: TAB_URL, 1.87 + onReady: function(tab) { 1.88 + assert.equal(tab.url, TAB_URL, 'opened correct tab'); 1.89 + assert.equal(isPrivate(tab), false); 1.90 + 1.91 + tab.close(function() { 1.92 + done(); 1.93 + }); 1.94 + } 1.95 + }); 1.96 +} 1.97 + 1.98 +// test that it is possible to open a non private tab in explicit case 1.99 +exports.testTabOpenPrivateOffExplicit = function(assert, done) { 1.100 + tabs.open({ 1.101 + url: TAB_URL, 1.102 + isPrivate: false, 1.103 + onReady: function(tab) { 1.104 + assert.equal(tab.url, TAB_URL, 'opened correct tab'); 1.105 + assert.equal(isPrivate(tab), false); 1.106 + 1.107 + tab.close(function() { 1.108 + done(); 1.109 + }); 1.110 + } 1.111 + }); 1.112 +} 1.113 + 1.114 +// test windows.open with isPrivate: true 1.115 +// test isPrivate on a window 1.116 +if (!is('Fennec')) { 1.117 + // test that it is possible to open a private window 1.118 + exports.testWindowOpenPrivate = function(assert, done) { 1.119 + windows.open({ 1.120 + url: TAB_URL, 1.121 + isPrivate: true, 1.122 + onOpen: function(window) { 1.123 + let tab = window.tabs[0]; 1.124 + tab.once('ready', function() { 1.125 + assert.equal(tab.url, TAB_URL, 'opened correct tab'); 1.126 + assert.equal(isPrivate(tab), isWindowPBSupported, 'tab is private'); 1.127 + 1.128 + window.close(function() { 1.129 + done(); 1.130 + }); 1.131 + }); 1.132 + } 1.133 + }); 1.134 + }; 1.135 + 1.136 + exports.testIsPrivateOnWindowOn = function(assert, done) { 1.137 + windows.open({ 1.138 + isPrivate: true, 1.139 + onOpen: function(window) { 1.140 + assert.equal(isPrivate(window), isWindowPBSupported, 'isPrivate for a window is true when it should be'); 1.141 + assert.equal(isPrivate(window.tabs[0]), isWindowPBSupported, 'isPrivate for a tab is false when it should be'); 1.142 + window.close(done); 1.143 + } 1.144 + }); 1.145 + }; 1.146 + 1.147 + exports.testIsPrivateOnWindowOffImplicit = function(assert, done) { 1.148 + windows.open({ 1.149 + onOpen: function(window) { 1.150 + assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be'); 1.151 + assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); 1.152 + window.close(done); 1.153 + } 1.154 + }) 1.155 + } 1.156 + 1.157 + exports.testIsPrivateOnWindowOffExplicit = function(assert, done) { 1.158 + windows.open({ 1.159 + isPrivate: false, 1.160 + onOpen: function(window) { 1.161 + assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be'); 1.162 + assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); 1.163 + window.close(done); 1.164 + } 1.165 + }) 1.166 + } 1.167 +}