Assert that listener count decreases after a remove event.

This commit is contained in:
Charlie Hess 2017-01-18 12:44:26 -08:00 committed by Kevin Sawicki
parent c213971a2d
commit f6410d3b77
3 changed files with 17 additions and 7 deletions

View file

@ -146,7 +146,7 @@ const throwRPCError = function (message) {
throw error throw error
} }
const rendererMissingErrorMessage = (meta, args, callIntoRenderer) => { const removeRemoteListenersAndLogWarning = (meta, args, callIntoRenderer) => {
let message = `Attempting to call a function in a renderer window that has been closed or released.` + let message = `Attempting to call a function in a renderer window that has been closed or released.` +
`\nFunction provided here: ${meta.location}` `\nFunction provided here: ${meta.location}`
@ -218,7 +218,7 @@ const unwrapArgs = function (sender, args) {
if (!sender.isDestroyed() && webContentsId === sender.getId()) { if (!sender.isDestroyed() && webContentsId === sender.getId()) {
sender.send('ELECTRON_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args)) sender.send('ELECTRON_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args))
} else { } else {
console.warn(rendererMissingErrorMessage(meta, args, callIntoRenderer)) console.warn(removeRemoteListenersAndLogWarning(meta, args, callIntoRenderer))
} }
} }

View file

@ -495,7 +495,7 @@ describe('ipc module', function () {
assert.equal(w.listenerCount('test'), 0) assert.equal(w.listenerCount('test'), 0)
}) })
it('shows a warning when a function is called in a destroyed renderer', (done) => { it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false show: false
}) })
@ -506,8 +506,10 @@ describe('ipc module', function () {
'Function provided here: remote-event-handler.html:11:33', 'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler' 'Remote event names: remote-handler, other-remote-handler'
].join('\n') ].join('\n')
const warningMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') const {warningMessage, listenerCountBefore, listenerCountAfter} =
ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.equal(warningMessage, expectedMessage) assert.equal(warningMessage, expectedMessage)
assert.equal(listenerCountAfter, listenerCountBefore - 1)
done() done()
}) })
w.webContents.reload() w.webContents.reload()

View file

@ -252,12 +252,20 @@ ipcMain.on('prevent-next-new-window', (event, id) => {
ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => { ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
const consoleWarn = console.warn const consoleWarn = console.warn
let lastWarning = null let warningMessage = null
console.warn = (message) => { console.warn = (message) => {
lastWarning = message warningMessage = message
} }
const contents = webContents.fromId(id) const contents = webContents.fromId(id)
const listenerCountBefore = contents.listenerCount(eventName)
contents.emit(eventName, {sender: contents}) contents.emit(eventName, {sender: contents})
event.returnValue = lastWarning const listenerCountAfter = contents.listenerCount(eventName)
event.returnValue = {
warningMessage,
listenerCountBefore,
listenerCountAfter
}
console.warn = consoleWarn console.warn = consoleWarn
}) })