forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-browser-window-affinity-spec.js
196 lines (176 loc) · 6.77 KB
/
api-browser-window-affinity-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict'
const { expect } = require('chai')
const path = require('path')
const { remote } = require('electron')
const { ipcMain, BrowserWindow } = remote
const { closeWindow } = require('./window-helpers')
describe('BrowserWindow with affinity module', () => {
const fixtures = path.resolve(__dirname, 'fixtures')
const myAffinityName = 'myAffinity'
const myAffinityNameUpper = 'MYAFFINITY'
const anotherAffinityName = 'anotherAffinity'
function createWindowWithWebPrefs (webPrefs) {
return new Promise((resolve, reject) => {
const w = new BrowserWindow({
show: false,
width: 400,
height: 400,
webPreferences: webPrefs || {}
})
w.webContents.on('did-finish-load', () => { resolve(w) })
w.loadFile(path.join(fixtures, 'api', 'blank.html'))
})
}
function testAffinityProcessIds (name, webPreferences = {}) {
describe(name, () => {
let mAffinityWindow
before(done => {
createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences })
.then((w) => {
mAffinityWindow = w
done()
})
})
after(done => {
closeWindow(mAffinityWindow, { assertSingleWindow: false }).then(() => {
mAffinityWindow = null
done()
})
})
it('should have a different process id than a default window', done => {
createWindowWithWebPrefs({ ...webPreferences })
.then(w => {
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs')
closeWindow(w, { assertSingleWindow: false }).then(() => { done() })
})
})
it(`should have a different process id than a window with a different affinity '${anotherAffinityName}'`, done => {
createWindowWithWebPrefs({ affinity: anotherAffinityName, ...webPreferences })
.then(w => {
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs')
closeWindow(w, { assertSingleWindow: false }).then(() => { done() })
})
})
it(`should have the same OS process id than a window with the same affinity '${myAffinityName}'`, done => {
createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences })
.then(w => {
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.equal(wcID, 'Should have the same OS process ID')
closeWindow(w, { assertSingleWindow: false }).then(() => { done() })
})
})
it(`should have the same OS process id than a window with an equivalent affinity '${myAffinityNameUpper}' (case insensitive)`, done => {
createWindowWithWebPrefs({ affinity: myAffinityNameUpper, ...webPreferences })
.then(w => {
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.equal(wcID, 'Should have the same OS process ID')
closeWindow(w, { assertSingleWindow: false }).then(() => { done() })
})
})
})
}
testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}'`)
testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}' and sandbox enabled`, { sandbox: true })
testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}' and nativeWindowOpen enabled`, { nativeWindowOpen: true })
describe(`BrowserWindow with an affinity : nodeIntegration=false`, () => {
const preload = path.join(fixtures, 'module', 'send-later.js')
const affinityWithNodeTrue = 'affinityWithNodeTrue'
const affinityWithNodeFalse = 'affinityWithNodeFalse'
function testNodeIntegration (present) {
return new Promise((resolve, reject) => {
ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
if (present) {
expect(typeofProcess).to.not.equal('undefined')
expect(typeofBuffer).to.not.equal('undefined')
} else {
expect(typeofProcess).to.equal('undefined')
expect(typeofBuffer).to.equal('undefined')
}
resolve()
})
})
}
it('disables node integration when specified to false', done => {
Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
})
]).then(args => {
closeWindow(args[1], { assertSingleWindow: false }).then(() => { done() })
})
})
it('disables node integration when first window is false', done => {
Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
})
]).then(args => {
const w1 = args[1]
return Promise.all([
testNodeIntegration(false),
w1,
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: true
})
])
}).then(ws => {
return Promise.all([
closeWindow(ws[1], { assertSingleWindow: false }),
closeWindow(ws[2], { assertSingleWindow: false })
])
}).then(() => { done() })
})
it('enables node integration when specified to true', done => {
Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
})
]).then(args => {
closeWindow(args[1], { assertSingleWindow: false }).then(() => { done() })
})
})
it('enables node integration when first window is true', done => {
Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
})
]).then(args => {
const w1 = args[1]
return Promise.all([
testNodeIntegration(true),
w1,
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: false
})
])
}).then(ws => {
return Promise.all([
closeWindow(ws[1], { assertSingleWindow: false }),
closeWindow(ws[2], { assertSingleWindow: false })
])
}).then(() => { done() })
})
})
})