test: move beforeunload tests to main runner and fix flake (#18432)

This commit is contained in:
Jeremy Apthorp 2019-05-29 13:38:14 -07:00 committed by GitHub
parent 9af5072115
commit babe2b68fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 53 deletions

View file

@ -4,15 +4,29 @@ const { BrowserWindow } = remote
const { emittedOnce } = require('./events-helpers')
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()
}
}
}
exports.closeWindow = async (window = null,
{ assertSingleWindow } = { assertSingleWindow: true }) => {
const windowExists = (window !== null) && !window.isDestroyed()
if (windowExists) {
const isClosed = emittedOnce(window, 'closed')
window.setClosable(true)
window.close()
await isClosed
}
await ensureWindowIsClosed(window)
if (assertSingleWindow) {
// Although we want to assert that no windows were left handing around
@ -22,9 +36,7 @@ exports.closeWindow = async (window = null,
const windows = BrowserWindow.getAllWindows()
for (const win of windows) {
if (win.id !== currentId) {
const closePromise = emittedOnce(win, 'closed')
win.close()
await closePromise
await ensureWindowIsClosed(win)
}
}
expect(windows).to.have.lengthOf(1)