api-desktop-capturer-spec.js 3.9 KB

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