api-desktop-capturer-spec.js 3.0 KB

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