Skip to content

Commit 281eefb

Browse files
committed
test: add setWindowOpenOverride tests
1 parent e57869b commit 281eefb

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

spec-main/guest-window-manager-spec.ts

+85-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BrowserWindow } from 'electron'
22
import { writeFileSync, readFileSync } from 'fs'
33
import { resolve } from 'path'
4-
import { expect } from 'chai'
4+
import { expect, assert } from 'chai'
55
import { closeAllWindows } from './window-helpers'
66

77
function genSnapshot (browserWindow: BrowserWindow, features: string) {
@@ -87,6 +87,90 @@ describe('new-window event', () => {
8787
}
8888
})
8989

90+
describe('webContents.setWindowOpenOverride', () => {
91+
const testConfig = {
92+
native: {
93+
browserWindowOptions: {
94+
show: false,
95+
webPreferences: {
96+
nativeWindowOpen: true
97+
}
98+
}
99+
},
100+
proxy: {
101+
browserWindowOptions: {
102+
show: false
103+
}
104+
}
105+
}
106+
107+
let testName : keyof typeof testConfig
108+
for (testName in testConfig) {
109+
let browserWindow: BrowserWindow
110+
const { browserWindowOptions } = testConfig[testName]
111+
112+
describe(testName, () => {
113+
beforeEach((done) => {
114+
browserWindow = new BrowserWindow(browserWindowOptions)
115+
browserWindow.loadURL('about:blank')
116+
browserWindow.on('ready-to-show', () => done())
117+
})
118+
119+
afterEach(closeAllWindows)
120+
121+
it('does not fire window creation events if an override returns false', (done) => {
122+
browserWindow.webContents.setWindowOpenOverride(() => false)
123+
browserWindow.webContents.on('new-window', () => {
124+
assert.fail('new-window should not to be called with an overridden window.open')
125+
})
126+
127+
browserWindow.webContents.on('did-create-window', () => {
128+
assert.fail('did-create-window should not to be called with an overridden window.open')
129+
})
130+
131+
browserWindow.webContents.executeJavaScript(`window.open('about:blank')`)
132+
133+
setTimeout(() => {
134+
done()
135+
}, 250)
136+
})
137+
138+
it('does fire window creation events if an override returns true', (done) => {
139+
browserWindow.webContents.setWindowOpenOverride(() => true)
140+
141+
let newWindowWasCalled = false
142+
let didCreateWindowWasCalled = false
143+
browserWindow.webContents.on('new-window', () => {
144+
newWindowWasCalled = true
145+
})
146+
147+
browserWindow.webContents.on('did-create-window', () => {
148+
didCreateWindowWasCalled = true
149+
})
150+
151+
browserWindow.webContents.executeJavaScript(`window.open('about:blank')`)
152+
153+
setTimeout(() => {
154+
expect(newWindowWasCalled).to.be.true()
155+
expect(didCreateWindowWasCalled).to.be.true()
156+
done()
157+
}, 250)
158+
})
159+
160+
it('fires window creation event with new options', (done) => {
161+
browserWindow.webContents.setWindowOpenOverride(() => ({ backgroundColor: 'pink' }))
162+
163+
browserWindow.webContents.on('did-create-window', (_window, { options }) => {
164+
expect(options.backgroundColor).to.equal('pink')
165+
done()
166+
})
167+
168+
browserWindow.webContents.executeJavaScript(`window.open('about:blank')`)
169+
})
170+
})
171+
}
172+
})
173+
90174
function stringifySnapshots (snapshots: any, pretty = false) {
91175
return JSON.stringify(snapshots, (key, value) => {
92176
if (['sender', 'webContents'].includes(key)) {

0 commit comments

Comments
 (0)