api-desktop-capturer-spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const chai = require('chai')
  2. const dirtyChai = require('dirty-chai')
  3. const chaiAsPromised = require('chai-as-promised')
  4. const { desktopCapturer, ipcRenderer, remote } = require('electron')
  5. const { screen } = remote
  6. const features = process.electronBinding('features')
  7. const { emittedOnce } = require('./events-helpers')
  8. const { expect } = chai
  9. chai.use(dirtyChai)
  10. chai.use(chaiAsPromised)
  11. const isCI = remote.getGlobal('isCi')
  12. describe('desktopCapturer', () => {
  13. before(function () {
  14. if (!features.isDesktopCapturerEnabled() || process.arch.indexOf('arm') === 0) {
  15. // It's been disabled during build time.
  16. this.skip()
  17. return
  18. }
  19. if (isCI && process.platform === 'win32') {
  20. this.skip()
  21. }
  22. })
  23. it('should return a non-empty array of sources', async () => {
  24. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] })
  25. expect(sources).to.be.an('array').that.is.not.empty()
  26. })
  27. it('throws an error for invalid options', async () => {
  28. const promise = desktopCapturer.getSources(['window', 'screen'])
  29. expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options')
  30. })
  31. it('does not throw an error when called more than once (regression)', async () => {
  32. const sources1 = await desktopCapturer.getSources({ types: ['window', 'screen'] })
  33. expect(sources1).to.be.an('array').that.is.not.empty()
  34. const sources2 = await desktopCapturer.getSources({ types: ['window', 'screen'] })
  35. expect(sources2).to.be.an('array').that.is.not.empty()
  36. })
  37. it('responds to subsequent calls of different options', async () => {
  38. const promise1 = desktopCapturer.getSources({ types: ['window'] })
  39. expect(promise1).to.not.eventually.be.rejected()
  40. const promise2 = desktopCapturer.getSources({ types: ['screen'] })
  41. expect(promise2).to.not.eventually.be.rejected()
  42. })
  43. it('returns an empty display_id for window sources on Windows and Mac', async () => {
  44. // Linux doesn't return any window sources.
  45. if (process.platform !== 'win32' && process.platform !== 'darwin') return
  46. const { BrowserWindow } = remote
  47. const w = new BrowserWindow({ width: 200, height: 200 })
  48. const sources = await desktopCapturer.getSources({ types: ['window'] })
  49. w.destroy()
  50. expect(sources).to.be.an('array').that.is.not.empty()
  51. for (const { display_id: displayId } of sources) {
  52. expect(displayId).to.be.a('string').and.be.empty()
  53. }
  54. })
  55. it('returns display_ids matching the Screen API on Windows and Mac', async () => {
  56. if (process.platform !== 'win32' && process.platform !== 'darwin') return
  57. const displays = screen.getAllDisplays()
  58. const sources = await desktopCapturer.getSources({ types: ['screen'] })
  59. expect(sources).to.be.an('array').of.length(displays.length)
  60. for (let i = 0; i < sources.length; i++) {
  61. expect(sources[i].display_id).to.equal(displays[i].id.toString())
  62. }
  63. it('returns empty sources when blocked', async () => {
  64. ipcRenderer.send('handle-next-desktop-capturer-get-sources')
  65. const sources = await desktopCapturer.getSources({ types: ['screen'] })
  66. expect(sources).to.be.empty()
  67. })
  68. })
  69. it('disabling thumbnail should return empty images', async () => {
  70. const { BrowserWindow } = remote
  71. const w = new BrowserWindow({ show: false, width: 200, height: 200 })
  72. const wShown = emittedOnce(w, 'show')
  73. w.show()
  74. await wShown
  75. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'], thumbnailSize: { width: 0, height: 0 } })
  76. w.destroy()
  77. expect(sources).to.be.an('array').that.is.not.empty()
  78. for (const { thumbnail: thumbnailImage } of sources) {
  79. expect(thumbnailImage).to.be.a('NativeImage')
  80. expect(thumbnailImage.isEmpty()).to.be.true()
  81. }
  82. })
  83. })