api-remote-spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import * as path from 'path'
  2. import { expect } from 'chai'
  3. import { closeWindow } from './window-helpers'
  4. import { ipcMain, BrowserWindow } from 'electron'
  5. describe('remote module', () => {
  6. const fixtures = path.join(__dirname, 'fixtures')
  7. let w = null as unknown as BrowserWindow
  8. beforeEach(async () => {
  9. w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
  10. await w.loadURL('about:blank')
  11. })
  12. afterEach(async () => {
  13. await closeWindow(w)
  14. })
  15. async function remotely(script: string) {
  16. // executeJavaScript never returns if the script throws an error, so catch
  17. // any errors manually.
  18. const assembledScript = `(function() {
  19. try {
  20. return { result: ${script} }
  21. } catch (e) {
  22. return { error: e.message }
  23. }
  24. })()`
  25. const {result, error} = await w.webContents.executeJavaScript(assembledScript)
  26. if (error) {
  27. throw new Error(error)
  28. }
  29. return result
  30. }
  31. describe('remote.getGlobal filtering', () => {
  32. it('can return custom values', async () => {
  33. w.webContents.once('remote-get-global', (event, name) => {
  34. event.returnValue = name
  35. })
  36. expect(await remotely(`require('electron').remote.getGlobal('test')`)).to.equal('test')
  37. })
  38. it('throws when no returnValue set', async () => {
  39. w.webContents.once('remote-get-global', (event, name) => {
  40. event.preventDefault()
  41. })
  42. await expect(remotely(`require('electron').remote.getGlobal('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
  43. })
  44. })
  45. describe('remote.getBuiltin filtering', () => {
  46. it('can return custom values', async () => {
  47. w.webContents.once('remote-get-builtin', (event, name) => {
  48. event.returnValue = name
  49. })
  50. expect(await remotely(`require('electron').remote.getBuiltin('test')`)).to.equal('test')
  51. })
  52. it('throws when no returnValue set', async () => {
  53. w.webContents.once('remote-get-builtin', (event, name) => {
  54. event.preventDefault()
  55. })
  56. await expect(remotely(`require('electron').remote.getBuiltin('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
  57. })
  58. })
  59. describe('remote.require filtering', () => {
  60. it('can return custom values', async () => {
  61. w.webContents.once('remote-require', (event, name) => {
  62. event.returnValue = name
  63. })
  64. expect(await remotely(`require('electron').remote.require('test')`)).to.equal('test')
  65. })
  66. it('throws when no returnValue set', async () => {
  67. w.webContents.once('remote-require', (event, name) => {
  68. event.preventDefault()
  69. })
  70. await expect(remotely(`require('electron').remote.require('test')`)).to.eventually.be.rejected(`Blocked remote.require('test')`)
  71. })
  72. })
  73. describe('remote.getCurrentWindow filtering', () => {
  74. it('can return custom value', async () => {
  75. w.webContents.once('remote-get-current-window', (e) => {
  76. e.returnValue = 'some window'
  77. })
  78. expect(await remotely(`require('electron').remote.getCurrentWindow()`)).to.equal('some window')
  79. })
  80. it('throws when no returnValue set', async () => {
  81. w.webContents.once('remote-get-current-window', (event) => {
  82. event.preventDefault()
  83. })
  84. await expect(remotely(`require('electron').remote.getCurrentWindow()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWindow()`)
  85. })
  86. })
  87. describe('remote.getCurrentWebContents filtering', () => {
  88. it('can return custom value', async () => {
  89. w.webContents.once('remote-get-current-web-contents', (event) => {
  90. event.returnValue = 'some web contents'
  91. })
  92. expect(await remotely(`require('electron').remote.getCurrentWebContents()`)).to.equal('some web contents')
  93. })
  94. it('throws when no returnValue set', async () => {
  95. w.webContents.once('remote-get-current-web-contents', (event) => {
  96. event.preventDefault()
  97. })
  98. await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`)
  99. })
  100. })
  101. describe('remote references', () => {
  102. it('render-view-deleted is sent when page is destroyed', (done) => {
  103. w.webContents.once('render-view-deleted' as any, () => {
  104. done()
  105. })
  106. w.destroy()
  107. })
  108. // The ELECTRON_BROWSER_CONTEXT_RELEASE message relies on this to work.
  109. it('message can be sent on exit when page is being navigated', (done) => {
  110. after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT') })
  111. ipcMain.once('SENT_ON_EXIT', () => {
  112. done()
  113. })
  114. w.webContents.once('did-finish-load', () => {
  115. w.webContents.loadURL('about:blank')
  116. })
  117. w.loadFile(path.join(fixtures, 'api', 'send-on-exit.html'))
  118. })
  119. })
  120. })