forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-browser-window-affinity-spec.ts
167 lines (148 loc) · 6.18 KB
/
api-browser-window-affinity-spec.ts
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
import { expect } from 'chai'
import * as path from 'path'
import { ipcMain, BrowserWindow, WebPreferences } from 'electron'
import { closeWindow } from './window-helpers'
describe('BrowserWindow with affinity module', () => {
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures')
const myAffinityName = 'myAffinity'
const myAffinityNameUpper = 'MYAFFINITY'
const anotherAffinityName = 'anotherAffinity'
async function createWindowWithWebPrefs (webPrefs: WebPreferences) {
const w = new BrowserWindow({
show: false,
width: 400,
height: 400,
webPreferences: webPrefs || {}
})
await w.loadFile(path.join(fixtures, 'api', 'blank.html'))
return w
}
function testAffinityProcessIds (name: string, webPreferences: WebPreferences = {}) {
describe(name, () => {
let mAffinityWindow: BrowserWindow
before(async () => {
mAffinityWindow = await createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences })
})
after(async () => {
await closeWindow(mAffinityWindow, { assertNotWindows: false })
mAffinityWindow = null as unknown as BrowserWindow
})
it('should have a different process id than a default window', async () => {
const w = await createWindowWithWebPrefs({ ...webPreferences })
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs')
await closeWindow(w, { assertNotWindows: false })
})
it(`should have a different process id than a window with a different affinity '${anotherAffinityName}'`, async () => {
const w = await createWindowWithWebPrefs({ affinity: anotherAffinityName, ...webPreferences })
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs')
await closeWindow(w, { assertNotWindows: false })
})
it(`should have the same OS process id than a window with the same affinity '${myAffinityName}'`, async () => {
const w = await createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences })
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.equal(wcID, 'Should have the same OS process ID')
await closeWindow(w, { assertNotWindows: false })
})
it(`should have the same OS process id than a window with an equivalent affinity '${myAffinityNameUpper}' (case insensitive)`, async () => {
const w = await createWindowWithWebPrefs({ affinity: myAffinityNameUpper, ...webPreferences })
const affinityID = mAffinityWindow.webContents.getOSProcessId()
const wcID = w.webContents.getOSProcessId()
expect(affinityID).to.equal(wcID, 'Should have the same OS process ID')
await closeWindow(w, { assertNotWindows: false })
})
})
}
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: boolean) {
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', async () => {
const [, w] = await Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
})
])
await closeWindow(w, { assertNotWindows: false })
})
it('disables node integration when first window is false', async () => {
const [, w1] = await Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
})
])
const [, w2] = await Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: true
})
])
await Promise.all([
closeWindow(w1, { assertNotWindows: false }),
closeWindow(w2, { assertNotWindows: false })
])
})
it('enables node integration when specified to true', async () => {
const [, w] = await Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
})
])
await closeWindow(w, { assertNotWindows: false })
})
it('enables node integration when first window is true', async () => {
const [, w1] = await Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
})
])
const [, w2] = await Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: false
})
])
await Promise.all([
closeWindow(w1, { assertNotWindows: false }),
closeWindow(w2, { assertNotWindows: false })
])
})
})
})