|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 'use strict'; |
|
5 |
|
6 const { pb, pbUtils } = require('./helper'); |
|
7 const { onFocus, openDialog, open } = require('sdk/window/utils'); |
|
8 const { open: openPromise, close, focus, promise } = require('sdk/window/helpers'); |
|
9 const { isPrivate } = require('sdk/private-browsing'); |
|
10 const { browserWindows: windows } = require('sdk/windows'); |
|
11 const { defer } = require('sdk/core/promise'); |
|
12 const tabs = require('sdk/tabs'); |
|
13 |
|
14 // test openDialog() from window/utils with private option |
|
15 // test isActive state in pwpb case |
|
16 // test isPrivate on ChromeWindow |
|
17 exports.testPerWindowPrivateBrowsingGetter = function(assert, done) { |
|
18 let win = openDialog({ |
|
19 private: true |
|
20 }); |
|
21 |
|
22 promise(win, 'DOMContentLoaded').then(function onload() { |
|
23 assert.equal(pbUtils.getMode(win), |
|
24 true, 'Newly opened window is in PB mode'); |
|
25 assert.ok(isPrivate(win), 'isPrivate(window) is true'); |
|
26 assert.equal(pb.isActive, false, 'PB mode is not active'); |
|
27 |
|
28 close(win).then(function() { |
|
29 assert.equal(pb.isActive, false, 'PB mode is not active'); |
|
30 done(); |
|
31 }); |
|
32 }); |
|
33 } |
|
34 |
|
35 // test open() from window/utils with private feature |
|
36 // test isActive state in pwpb case |
|
37 // test isPrivate on ChromeWindow |
|
38 exports.testPerWindowPrivateBrowsingGetter = function(assert, done) { |
|
39 let win = open('chrome://browser/content/browser.xul', { |
|
40 features: { |
|
41 private: true |
|
42 } |
|
43 }); |
|
44 |
|
45 promise(win, 'DOMContentLoaded').then(function onload() { |
|
46 assert.equal(pbUtils.getMode(win), |
|
47 true, 'Newly opened window is in PB mode'); |
|
48 assert.ok(isPrivate(win), 'isPrivate(window) is true'); |
|
49 assert.equal(pb.isActive, false, 'PB mode is not active'); |
|
50 |
|
51 close(win).then(function() { |
|
52 assert.equal(pb.isActive, false, 'PB mode is not active'); |
|
53 done(); |
|
54 }); |
|
55 }); |
|
56 } |
|
57 |
|
58 exports.testIsPrivateOnWindowOpen = function(assert, done) { |
|
59 windows.open({ |
|
60 isPrivate: true, |
|
61 onOpen: function(window) { |
|
62 assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be'); |
|
63 assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); |
|
64 window.close(done); |
|
65 } |
|
66 }); |
|
67 } |
|
68 |
|
69 exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) { |
|
70 // open a private window |
|
71 openPromise(null, { |
|
72 features: { |
|
73 private: true, |
|
74 chrome: true, |
|
75 titlebar: true, |
|
76 toolbar: true |
|
77 } |
|
78 }).then(focus).then(function(window) { |
|
79 let { promise, resolve } = defer(); |
|
80 |
|
81 assert.equal(isPrivate(window), true, 'the only open window is private'); |
|
82 |
|
83 windows.open({ |
|
84 url: 'about:blank', |
|
85 onOpen: function(w) { |
|
86 assert.equal(isPrivate(w), false, 'new test window is not private'); |
|
87 w.close(function() resolve(window)); |
|
88 } |
|
89 }); |
|
90 |
|
91 return promise; |
|
92 }).then(close). |
|
93 then(done, assert.fail); |
|
94 }; |
|
95 |
|
96 exports.testOpenTabWithPrivateWindow = function(assert, done) { |
|
97 function start() { |
|
98 openPromise(null, { |
|
99 features: { |
|
100 private: true, |
|
101 toolbar: true |
|
102 } |
|
103 }).then(focus).then(function(window) { |
|
104 let { promise, resolve } = defer(); |
|
105 assert.equal(isPrivate(window), true, 'the focused window is private'); |
|
106 |
|
107 tabs.open({ |
|
108 url: 'about:blank', |
|
109 onOpen: function(tab) { |
|
110 assert.equal(isPrivate(tab), false, 'the opened tab is not private'); |
|
111 // not closing this tab on purpose.. for now... |
|
112 // we keep this tab open because we closed all windows |
|
113 // and must keep a non-private window open at end of this test for next ones. |
|
114 resolve(window); |
|
115 } |
|
116 }); |
|
117 |
|
118 return promise; |
|
119 }).then(close).then(done, assert.fail); |
|
120 } |
|
121 |
|
122 (function closeWindows() { |
|
123 if (windows.length > 0) { |
|
124 return windows.activeWindow.close(closeWindows); |
|
125 } |
|
126 assert.pass('all pre test windows have been closed'); |
|
127 return start(); |
|
128 })() |
|
129 }; |
|
130 |
|
131 exports.testIsPrivateOnWindowOff = function(assert, done) { |
|
132 windows.open({ |
|
133 onOpen: function(window) { |
|
134 assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be'); |
|
135 assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be'); |
|
136 window.close(done); |
|
137 } |
|
138 }) |
|
139 } |