api-desktop-capturer-spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // TODO(codebytere): remove when promisification is complete
  28. it('should return a non-empty array of sources (callback)', (done) => {
  29. desktopCapturer.getSources({ types: ['window', 'screen'] }, (err, sources) => {
  30. expect(sources).to.be.an('array').that.is.not.empty()
  31. expect(err).to.be.null()
  32. done()
  33. })
  34. })
  35. it('throws an error for invalid options', async () => {
  36. const promise = desktopCapturer.getSources(['window', 'screen'])
  37. expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options')
  38. })
  39. it('does not throw an error when called more than once (regression)', async () => {
  40. const sources1 = await desktopCapturer.getSources({ types: ['window', 'screen'] })
  41. expect(sources1).to.be.an('array').that.is.not.empty()
  42. const sources2 = await desktopCapturer.getSources({ types: ['window', 'screen'] })
  43. expect(sources2).to.be.an('array').that.is.not.empty()
  44. })
  45. it('responds to subsequent calls of different options', async () => {
  46. const promise1 = desktopCapturer.getSources({ types: ['window'] })
  47. expect(promise1).to.not.eventually.be.rejected()
  48. const promise2 = desktopCapturer.getSources({ types: ['screen'] })
  49. expect(promise2).to.not.eventually.be.rejected()
  50. })
  51. // TODO(codebytere): remove when promisification is complete
  52. it('responds to subsequent calls of different options (callback)', (done) => {
  53. let callCount = 0
  54. const callback = (err, sources) => {
  55. callCount++
  56. expect(err).to.be.null()
  57. expect(sources).to.not.be.null()
  58. if (callCount === 2) done()
  59. }
  60. desktopCapturer.getSources({ types: ['window'] }, callback)
  61. desktopCapturer.getSources({ types: ['screen'] }, callback)
  62. })
  63. it('returns an empty display_id for window sources on Windows and Mac', async () => {
  64. // Linux doesn't return any window sources.
  65. if (process.platform !== 'win32' && process.platform !== 'darwin') return
  66. const { BrowserWindow } = remote
  67. const w = new BrowserWindow({ width: 200, height: 200 })
  68. const sources = await desktopCapturer.getSources({ types: ['window'] })
  69. w.destroy()
  70. expect(sources).to.be.an('array').that.is.not.empty()
  71. for (const { display_id: displayId } of sources) {
  72. expect(displayId).to.be.a('string').and.be.empty()
  73. }
  74. })
  75. it('returns display_ids matching the Screen API on Windows and Mac', async () => {
  76. if (process.platform !== 'win32' && process.platform !== 'darwin') return
  77. const displays = screen.getAllDisplays()
  78. const sources = await desktopCapturer.getSources({ types: ['screen'] })
  79. expect(sources).to.be.an('array').of.length(displays.length)
  80. for (let i = 0; i < sources.length; i++) {
  81. expect(sources[i].display_id).to.equal(displays[i].id.toString())
  82. }
  83. it('returns empty sources when blocked', async () => {
  84. ipcRenderer.send('handle-next-desktop-capturer-get-sources')
  85. const sources = await desktopCapturer.getSources({ types: ['screen'] })
  86. expect(sources).to.be.empty()
  87. })
  88. })
  89. it('disabling thumbnail should return empty images', async () => {
  90. const { BrowserWindow } = remote
  91. const w = new BrowserWindow({ show: false, width: 200, height: 200 })
  92. const wShown = emittedOnce(w, 'show')
  93. w.show()
  94. await wShown
  95. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'], thumbnailSize: { width: 0, height: 0 } })
  96. w.destroy()
  97. expect(sources).to.be.an('array').that.is.not.empty()
  98. for (const { thumbnail: thumbnailImage } of sources) {
  99. expect(thumbnailImage).to.be.a('NativeImage')
  100. expect(thumbnailImage.isEmpty()).to.be.true()
  101. }
  102. })
  103. })