test: asyncawaitify one of sandbox related tests (backport: 3-0-x) (#15281)
* test: asyncawaitify one of sandbox related tests (#15252)
(cherry picked from commit b3f134de06
)
* test: backport "openTheWindow" function to "BrowserWindow module" specs
This commit is contained in:
parent
ead294fe13
commit
ef4a7e22fd
3 changed files with 53 additions and 33 deletions
|
@ -24,6 +24,24 @@ describe('BrowserWindow module', () => {
|
|||
let server
|
||||
let postData
|
||||
|
||||
const defaultOptions = {
|
||||
show: false,
|
||||
width: 400,
|
||||
height: 400,
|
||||
webPreferences: {
|
||||
backgroundThrottling: false
|
||||
}
|
||||
}
|
||||
|
||||
const openTheWindow = async (options = defaultOptions) => {
|
||||
// The `afterEach` hook isn't called if a test fails,
|
||||
// we should make sure that the window is closed ourselves.
|
||||
await closeTheWindow()
|
||||
|
||||
w = new BrowserWindow(options)
|
||||
return w
|
||||
}
|
||||
|
||||
const closeTheWindow = function () {
|
||||
return closeWindow(w).then(() => { w = null })
|
||||
}
|
||||
|
@ -1292,43 +1310,43 @@ describe('BrowserWindow module', () => {
|
|||
})
|
||||
})
|
||||
|
||||
it('should open windows in another domain with cross-scripting disabled', (done) => {
|
||||
w.destroy()
|
||||
w = new BrowserWindow({
|
||||
it('should open windows in another domain with cross-scripting disabled', async () => {
|
||||
const w = await openTheWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
sandbox: true,
|
||||
preload: preload
|
||||
preload
|
||||
}
|
||||
})
|
||||
|
||||
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
|
||||
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open-external')
|
||||
const pageUrl = 'file://' + htmlPath
|
||||
let popupWindow
|
||||
w.loadURL(pageUrl)
|
||||
w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
|
||||
assert.equal(url, 'http://www.google.com/#q=electron')
|
||||
assert.equal(options.width, 505)
|
||||
assert.equal(options.height, 605)
|
||||
ipcMain.once('child-loaded', function (event, openerIsNull, html) {
|
||||
assert(openerIsNull)
|
||||
assert.equal(html, '<h1>http://www.google.com/#q=electron</h1>')
|
||||
ipcMain.once('answer', function (event, exceptionMessage) {
|
||||
assert(/Blocked a frame with origin/.test(exceptionMessage))
|
||||
w.loadURL(`file://${htmlPath}`)
|
||||
const expectedPopupUrl = 'http://www.google.com/#q=electron' // Set in the "sandbox.html".
|
||||
|
||||
// FIXME this popup window should be closed in sandbox.html
|
||||
closeWindow(popupWindow, {assertSingleWindow: false}).then(() => {
|
||||
popupWindow = null
|
||||
done()
|
||||
})
|
||||
})
|
||||
w.webContents.send('child-loaded')
|
||||
})
|
||||
})
|
||||
// The page is going to open a popup that it won't be able to close.
|
||||
// We have to close it from here later.
|
||||
// XXX(alexeykuzmin): It will leak if the test fails too soon.
|
||||
const [, popupWindow] = await emittedOnce(app, 'browser-window-created')
|
||||
|
||||
app.once('browser-window-created', function (event, window) {
|
||||
popupWindow = window
|
||||
})
|
||||
// Wait for a message from the popup's preload script.
|
||||
const [, openerIsNull, html, locationHref] = await emittedOnce(ipcMain, 'child-loaded')
|
||||
expect(openerIsNull).to.be.true('window.opener is not null')
|
||||
expect(html).to.equal(`<h1>${expectedPopupUrl}</h1>`,
|
||||
'looks like a http: request has not been intercepted locally')
|
||||
expect(locationHref).to.equal(expectedPopupUrl)
|
||||
|
||||
// Ask the page to access the popup.
|
||||
w.webContents.send('touch-the-popup')
|
||||
const [, exceptionMessage] = await emittedOnce(ipcMain, 'answer')
|
||||
|
||||
// We don't need the popup anymore, and its parent page can't close it,
|
||||
// so let's close it from here before we run any checks.
|
||||
await closeWindow(popupWindow, { assertSingleWindow: false })
|
||||
|
||||
expect(exceptionMessage).to.be.a('string',
|
||||
`child's .document is accessible from its parent window`)
|
||||
expect(exceptionMessage).to.match(/^Blocked a frame with origin/)
|
||||
})
|
||||
|
||||
it('should inherit the sandbox setting in opened windows', (done) => {
|
||||
|
|
10
spec/fixtures/api/sandbox.html
vendored
10
spec/fixtures/api/sandbox.html
vendored
|
@ -80,12 +80,14 @@
|
|||
},
|
||||
'window-open-external': () => {
|
||||
addEventListener('load', () => {
|
||||
ipcRenderer.once('child-loaded', (e) => {
|
||||
ipcRenderer.once('touch-the-popup', () => {
|
||||
let errorMessage = null
|
||||
try {
|
||||
let childDoc = popup.document
|
||||
} catch (e) {
|
||||
ipcRenderer.send('answer', e.message)
|
||||
const childDoc = popup.document
|
||||
} catch (error) {
|
||||
errorMessage = error.message
|
||||
}
|
||||
ipcRenderer.send('answer', errorMessage)
|
||||
})
|
||||
popup = open('http://www.google.com/#q=electron', '', 'top=65,left=55,width=505,height=605')
|
||||
})
|
||||
|
|
2
spec/fixtures/module/preload-sandbox.js
vendored
2
spec/fixtures/module/preload-sandbox.js
vendored
|
@ -16,7 +16,7 @@
|
|||
}
|
||||
} else if (location.href !== 'about:blank') {
|
||||
addEventListener('DOMContentLoaded', () => {
|
||||
ipcRenderer.send('child-loaded', window.opener == null, document.body.innerHTML)
|
||||
ipcRenderer.send('child-loaded', window.opener == null, document.body.innerHTML, location.href)
|
||||
}, false)
|
||||
}
|
||||
})()
|
||||
|
|
Loading…
Reference in a new issue