refactor: use separate ipc-renderer-internal / ipc-main-internal APIs for Electron internals (#13940)

This commit is contained in:
Milan Burda 2018-10-06 13:48:00 +02:00 committed by Samuel Attard
parent f7122610cc
commit b50f86ef43
49 changed files with 322 additions and 133 deletions

View file

@ -3,7 +3,9 @@
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const path = require('path')
const cp = require('child_process')
const { closeWindow } = require('./window-helpers')
const { emittedOnce } = require('./events-helpers')
const { expect } = chai
chai.use(dirtyChai)
@ -55,18 +57,6 @@ describe('ipc main module', () => {
})
})
it('throws an error when removing all the listeners', () => {
ipcMain.on('test-event', () => {})
expect(ipcMain.listenerCount('test-event')).to.equal(1)
expect(() => {
ipcMain.removeAllListeners()
}).to.throw(/Removing all listeners from ipcMain will make Electron internals stop working/)
ipcMain.removeAllListeners('test-event')
expect(ipcMain.listenerCount('test-event')).to.equal(0)
})
describe('remote objects registry', () => {
it('does not dereference until the render view is deleted (regression)', (done) => {
w = new BrowserWindow({ show: false })
@ -80,4 +70,20 @@ describe('ipc main module', () => {
w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
})
})
describe('ipcMain.on', () => {
it('is not used for internals', async () => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'ipc-main-listeners')
const electronPath = remote.getGlobal('process').execPath
const appProcess = cp.spawn(electronPath, [appPath])
let output = ''
appProcess.stdout.on('data', (data) => { output += data })
await emittedOnce(appProcess.stdout, 'end')
output = JSON.parse(output)
expect(output).to.deep.equal(['error'])
})
})
})

View file

@ -5,6 +5,7 @@ const dirtyChai = require('dirty-chai')
const http = require('http')
const path = require('path')
const { closeWindow } = require('./window-helpers')
const { emittedOnce } = require('./events-helpers')
const { expect } = chai
chai.use(dirtyChai)
@ -225,15 +226,16 @@ describe('ipc renderer module', () => {
})
})
it('throws an error when removing all the listeners', () => {
ipcRenderer.on('test-event', () => {})
expect(ipcRenderer.listenerCount('test-event')).to.equal(1)
describe('ipcRenderer.on', () => {
it('is not used for internals', async () => {
w = new BrowserWindow({ show: false })
w.loadURL('about:blank')
expect(() => {
ipcRenderer.removeAllListeners()
}).to.throw(/Removing all listeners from ipcRenderer will make Electron internals stop working/)
await emittedOnce(w.webContents, 'did-finish-load')
ipcRenderer.removeAllListeners('test-event')
expect(ipcRenderer.listenerCount('test-event')).to.equal(0)
const script = `require('electron').ipcRenderer.eventNames()`
const result = await w.webContents.executeJavaScript(script)
expect(result).to.deep.equal([])
})
})
})

View file

@ -0,0 +1,10 @@
const { app, ipcMain } = require('electron')
app.on('ready', () => {
process.stdout.write(JSON.stringify(ipcMain.eventNames()))
process.stdout.end()
setImmediate(() => {
app.quit()
})
})

View file

@ -0,0 +1,4 @@
{
"name": "ipc-main-listeners",
"main": "main.js"
}