2018-09-13 16:10:51 +00:00
|
|
|
const { expect } = require('chai')
|
2019-04-16 20:08:11 +00:00
|
|
|
const { remote } = require('electron')
|
|
|
|
const { BrowserWindow } = remote
|
2016-11-29 19:06:22 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { emittedOnce } = require('./events-helpers')
|
2018-06-21 14:43:15 +00:00
|
|
|
|
2019-05-29 20:38:14 +00:00
|
|
|
async function ensureWindowIsClosed (window) {
|
|
|
|
if (window && !window.isDestroyed()) {
|
|
|
|
if (window.webContents && !window.webContents.isDestroyed()) {
|
|
|
|
// If a window isn't destroyed already, and it has non-destroyed WebContents,
|
|
|
|
// then calling destroy() won't immediately destroy it, as it may have
|
|
|
|
// <webview> children which need to be destroyed first. In that case, we
|
|
|
|
// await the 'closed' event which signals the complete shutdown of the
|
|
|
|
// window.
|
|
|
|
const isClosed = emittedOnce(window, 'closed')
|
|
|
|
window.destroy()
|
|
|
|
await isClosed
|
|
|
|
} else {
|
|
|
|
// If there's no WebContents or if the WebContents is already destroyed,
|
|
|
|
// then the 'closed' event has already been emitted so there's nothing to
|
|
|
|
// wait for.
|
|
|
|
window.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 14:43:15 +00:00
|
|
|
exports.closeWindow = async (window = null,
|
2018-09-13 16:10:51 +00:00
|
|
|
{ assertSingleWindow } = { assertSingleWindow: true }) => {
|
2019-05-29 20:38:14 +00:00
|
|
|
await ensureWindowIsClosed(window)
|
2018-06-21 14:43:15 +00:00
|
|
|
|
|
|
|
if (assertSingleWindow) {
|
2019-04-16 20:08:11 +00:00
|
|
|
// Although we want to assert that no windows were left handing around
|
|
|
|
// we also want to clean up the left over windows so that no future
|
|
|
|
// tests fail as a side effect
|
|
|
|
const currentId = remote.getCurrentWindow().id
|
|
|
|
const windows = BrowserWindow.getAllWindows()
|
|
|
|
for (const win of windows) {
|
|
|
|
if (win.id !== currentId) {
|
2019-05-29 20:38:14 +00:00
|
|
|
await ensureWindowIsClosed(win)
|
2019-04-16 20:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(windows).to.have.lengthOf(1)
|
2016-08-03 19:47:53 +00:00
|
|
|
}
|
|
|
|
}
|