api-ipc-main-spec.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. const assert = require('assert')
  3. const path = require('path')
  4. const {closeWindow} = require('./window-helpers')
  5. const {remote} = require('electron')
  6. const {ipcMain, BrowserWindow} = remote
  7. describe('ipc main module', () => {
  8. const fixtures = path.join(__dirname, 'fixtures')
  9. let w = null
  10. afterEach(() => closeWindow(w).then(() => { w = null }))
  11. describe('ipc.sendSync', () => {
  12. afterEach(() => { ipcMain.removeAllListeners('send-sync-message') })
  13. it('does not crash when reply is not sent and browser is destroyed', (done) => {
  14. w = new BrowserWindow({ show: false })
  15. ipcMain.once('send-sync-message', (event) => {
  16. event.returnValue = null
  17. done()
  18. })
  19. w.loadURL(`file://${path.join(fixtures, 'api', 'send-sync-message.html')}`)
  20. })
  21. it('does not crash when reply is sent by multiple listeners', (done) => {
  22. w = new BrowserWindow({ show: false })
  23. ipcMain.on('send-sync-message', (event) => {
  24. event.returnValue = null
  25. })
  26. ipcMain.on('send-sync-message', (event) => {
  27. event.returnValue = null
  28. done()
  29. })
  30. w.loadURL(`file://${path.join(fixtures, 'api', 'send-sync-message.html')}`)
  31. })
  32. })
  33. describe('remote listeners', () => {
  34. it('can be added and removed correctly', () => {
  35. w = new BrowserWindow({ show: false })
  36. const listener = () => {}
  37. w.on('test', listener)
  38. assert.equal(w.listenerCount('test'), 1)
  39. w.removeListener('test', listener)
  40. assert.equal(w.listenerCount('test'), 0)
  41. })
  42. })
  43. it('throws an error when removing all the listeners', () => {
  44. ipcMain.on('test-event', () => {})
  45. assert.equal(ipcMain.listenerCount('test-event'), 1)
  46. assert.throws(() => {
  47. ipcMain.removeAllListeners()
  48. }, /Removing all listeners from ipcMain will make Electron internals stop working/)
  49. ipcMain.removeAllListeners('test-event')
  50. assert.equal(ipcMain.listenerCount('test-event'), 0)
  51. })
  52. describe('remote objects registry', () => {
  53. it('does not dereference until the render view is deleted (regression)', (done) => {
  54. w = new BrowserWindow({ show: false })
  55. ipcMain.once('error-message', (event, message) => {
  56. assert(message.startsWith('Cannot call function \'getURL\' on missing remote object'), message)
  57. done()
  58. })
  59. w.loadURL(`file://${path.join(fixtures, 'api', 'render-view-deleted.html')}`)
  60. })
  61. })
  62. })