api-ipc-main-spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { expect } from 'chai'
  2. import * as path from 'path'
  3. import * as cp from 'child_process'
  4. import { closeAllWindows } from './window-helpers'
  5. import { emittedOnce } from './events-helpers'
  6. import { ipcMain, BrowserWindow } from 'electron'
  7. describe('ipc main module', () => {
  8. const fixtures = path.join(__dirname, '..', 'spec', 'fixtures')
  9. afterEach(closeAllWindows)
  10. describe('ipc.sendSync', () => {
  11. afterEach(() => { ipcMain.removeAllListeners('send-sync-message') })
  12. it('does not crash when reply is not sent and browser is destroyed', (done) => {
  13. const w = new BrowserWindow({
  14. show: false,
  15. webPreferences: {
  16. nodeIntegration: true
  17. }
  18. })
  19. ipcMain.once('send-sync-message', (event) => {
  20. event.returnValue = null
  21. done()
  22. })
  23. w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
  24. })
  25. it('does not crash when reply is sent by multiple listeners', (done) => {
  26. const w = new BrowserWindow({
  27. show: false,
  28. webPreferences: {
  29. nodeIntegration: true
  30. }
  31. })
  32. ipcMain.on('send-sync-message', (event) => {
  33. event.returnValue = null
  34. })
  35. ipcMain.on('send-sync-message', (event) => {
  36. event.returnValue = null
  37. done()
  38. })
  39. w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
  40. })
  41. })
  42. describe('remote objects registry', () => {
  43. it('does not dereference until the render view is deleted (regression)', (done) => {
  44. const w = new BrowserWindow({
  45. show: false,
  46. webPreferences: {
  47. nodeIntegration: true
  48. }
  49. })
  50. ipcMain.once('error-message', (event, message) => {
  51. expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
  52. done()
  53. })
  54. w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
  55. })
  56. })
  57. describe('ipcMain.on', () => {
  58. it('is not used for internals', async () => {
  59. const appPath = path.join(fixtures, 'api', 'ipc-main-listeners')
  60. const electronPath = process.execPath
  61. const appProcess = cp.spawn(electronPath, [appPath])
  62. let output = ''
  63. appProcess.stdout.on('data', (data) => { output += data })
  64. await emittedOnce(appProcess.stdout, 'end')
  65. output = JSON.parse(output)
  66. expect(output).to.deep.equal(['error'])
  67. })
  68. })
  69. })