electron/spec/window-helpers.js
Samuel Attard 78411db4b5
fix: maintain a ref count for objects sent over remote (#17464)
* spec: clean up after a failed window count assertion

Previously when this assertion failed all tests that ran after the
failed assertion also failed.  This ensure that the assertion fails for
the test that actually caused the issue but cleans up the left-over
windows so that future tests do not fail.

* fix: maintain a ref count for objects sent over remote

Previously there was a race condition where a GC could occur in the
renderer process between the main process sending a meta.id and the
renderer pulling the proxy out its weakmap to stop it being GC'ed.

This fixes that race condition by maintaining a "sent" ref count in the
object registry and a "received" ref count in the object cache on the
renderer side.  The deref request now sends the number of refs the
renderer thinks it owns, if the number does not match the value in the
object registry it is assumed that there is an IPC message containing a
new reference in flight and this race condition was hit.

The browser side ref count is then reduced and we wait for the new deref
message.  This guaruntees that an object will only be removed from the
registry if every reference we sent has been guarunteed to be unreffed.
2019-04-16 16:08:11 -04:00

39 lines
1.2 KiB
JavaScript

const { expect } = require('chai')
const { remote } = require('electron')
const { BrowserWindow } = remote
const { emittedOnce } = require('./events-helpers')
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
}
if (assertSingleWindow) {
// 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) {
const closePromise = emittedOnce(win, 'closed')
win.close()
await closePromise
}
}
expect(windows).to.have.lengthOf(1)
}
}
exports.waitForWebContentsToLoad = async (webContents) => {
const didFinishLoadPromise = emittedOnce(webContents, 'did-finish-load')
if (webContents.isLoadingMainFrame()) {
await didFinishLoadPromise
}
}