electron/spec-main/api-ipc-main-spec.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-07-15 08:23:59 +00:00
import { expect } from 'chai'
import * as path from 'path'
import * as cp from 'child_process'
import { closeAllWindows } from './window-helpers'
import { emittedOnce } from './events-helpers'
import { ipcMain, BrowserWindow } from 'electron'
2017-12-01 20:57:41 +00:00
2017-12-01 21:01:03 +00:00
describe('ipc main module', () => {
2019-07-15 08:23:59 +00:00
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures')
2017-12-01 20:57:41 +00:00
2019-07-15 08:23:59 +00:00
afterEach(closeAllWindows)
2017-12-01 20:57:41 +00:00
describe('ipc.sendSync', () => {
afterEach(() => { ipcMain.removeAllListeners('send-sync-message') })
it('does not crash when reply is not sent and browser is destroyed', (done) => {
2019-07-15 08:23:59 +00:00
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
2017-12-01 20:57:41 +00:00
ipcMain.once('send-sync-message', (event) => {
event.returnValue = null
done()
})
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
2017-12-01 20:57:41 +00:00
})
it('does not crash when reply is sent by multiple listeners', (done) => {
2019-07-15 08:23:59 +00:00
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
2017-12-01 20:57:41 +00:00
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null
})
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null
done()
})
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
2017-12-01 20:57:41 +00:00
})
})
describe('ipcMain.on', () => {
it('is not used for internals', async () => {
2019-07-15 08:23:59 +00:00
const appPath = path.join(fixtures, 'api', 'ipc-main-listeners')
const electronPath = 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'])
})
})
2017-12-01 20:57:41 +00:00
})