From 45986405b85c663d18a3ac30c966c3eee1101d6e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 12 Jan 2017 16:31:02 -0800 Subject: [PATCH] Assert remote event names --- lib/browser/rpc-server.js | 2 +- spec/api-ipc-spec.js | 20 +++++++++++++------ ...ad-page.html => remote-event-handler.html} | 5 +++-- spec/static/main.js | 10 ++++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) rename spec/fixtures/api/{reload-page.html => remote-event-handler.html} (60%) diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index d097d9103dd8..5a871709ecea 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -161,7 +161,7 @@ const rendererMissingErrorMessage = (meta, args, callIntoRenderer) => { }) if (remoteEvents.length > 0) { - message += `\nRemote event names: ${remoteEvents.join(' ')}` + message += `\nRemote event names: ${remoteEvents.join(', ')}` } return message diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index eab88033bcca..47f9b98bf52b 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -499,12 +499,20 @@ describe('ipc module', function () { w = new BrowserWindow({ show: false }) - w.loadURL('file://' + path.join(fixtures, 'api', 'reload-page.html')) - setTimeout(() => { - assert.throws(() => w.webContents.emit('remote-handler'), - /Attempting to call a function in a renderer window that has been closed or released./) - done() - }, 400) + w.webContents.once('did-finish-load', () => { + w.webContents.once('did-finish-load', () => { + const expectedMessage = [ + 'Attempting to call a function in a renderer window that has been closed or released.', + 'Function provided here: remote-event-handler.html:11:33', + 'Remote event names: remote-handler, other-remote-handler' + ].join('\n') + const errorMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') + assert.equal(errorMessage, expectedMessage) + done() + }) + w.webContents.reload() + }) + w.loadURL('file://' + path.join(fixtures, 'api', 'remote-event-handler.html')) }) }) diff --git a/spec/fixtures/api/reload-page.html b/spec/fixtures/api/remote-event-handler.html similarity index 60% rename from spec/fixtures/api/reload-page.html rename to spec/fixtures/api/remote-event-handler.html index 240548cb8d8f..30c3cfb36ad7 100644 --- a/spec/fixtures/api/reload-page.html +++ b/spec/fixtures/api/remote-event-handler.html @@ -7,8 +7,9 @@ const {remote} = require('electron') const browserWindow = remote.getCurrentWindow() - browserWindow.webContents.on('remote-handler', () => { }) - browserWindow.reload() + const handler = () => {} + browserWindow.webContents.on('remote-handler', handler) + browserWindow.webContents.on('other-remote-handler', handler) diff --git a/spec/static/main.js b/spec/static/main.js index 9612ed25cf05..cb14f9d9aec5 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -249,3 +249,13 @@ ipcMain.on('create-window-with-options-cycle', (event) => { ipcMain.on('prevent-next-new-window', (event, id) => { webContents.fromId(id).once('new-window', event => event.preventDefault()) }) + +ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => { + const contents = webContents.fromId(id) + try { + contents.emit(eventName, {sender: contents}) + event.returnValue = null + } catch (error) { + event.returnValue = error.message + } +})