api-desktop-capturer-spec.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const assert = require('assert')
  2. const {desktopCapturer, remote, screen} = require('electron')
  3. const isCI = remote.getGlobal('isCi')
  4. describe('desktopCapturer', () => {
  5. before(function () {
  6. if (isCI && process.platform === 'win32') {
  7. this.skip()
  8. }
  9. })
  10. it('should return a non-empty array of sources', (done) => {
  11. desktopCapturer.getSources({
  12. types: ['window', 'screen']
  13. }, (error, sources) => {
  14. assert.equal(error, null)
  15. assert.notEqual(sources.length, 0)
  16. done()
  17. })
  18. })
  19. it('throws an error for invalid options', (done) => {
  20. desktopCapturer.getSources(['window', 'screen'], (error) => {
  21. assert.equal(error.message, 'Invalid options')
  22. done()
  23. })
  24. })
  25. it('does not throw an error when called more than once (regression)', (done) => {
  26. let callCount = 0
  27. const callback = (error, sources) => {
  28. callCount++
  29. assert.equal(error, null)
  30. assert.notEqual(sources.length, 0)
  31. if (callCount === 2) done()
  32. }
  33. desktopCapturer.getSources({types: ['window', 'screen']}, callback)
  34. desktopCapturer.getSources({types: ['window', 'screen']}, callback)
  35. })
  36. it('responds to subsequent calls of different options', (done) => {
  37. let callCount = 0
  38. const callback = (error, sources) => {
  39. callCount++
  40. assert.equal(error, null)
  41. if (callCount === 2) done()
  42. }
  43. desktopCapturer.getSources({types: ['window']}, callback)
  44. desktopCapturer.getSources({types: ['screen']}, callback)
  45. })
  46. it('returns an empty display_id for window sources on Windows and Mac', (done) => {
  47. // Linux doesn't return any window sources.
  48. if (process.platform !== 'win32' && process.platform !== 'darwin') {
  49. return done()
  50. }
  51. desktopCapturer.getSources({types: ['window']}, (error, sources) => {
  52. assert.equal(error, null)
  53. assert.notEqual(sources.length, 0)
  54. sources.forEach((source) => { assert.equal(source.display_id.length, 0) })
  55. done()
  56. })
  57. })
  58. it('returns display_ids matching the Screen API on Windows and Mac', (done) => {
  59. if (process.platform !== 'win32' && process.platform !== 'darwin') {
  60. return done()
  61. }
  62. const displays = screen.getAllDisplays()
  63. desktopCapturer.getSources({types: ['screen']}, (error, sources) => {
  64. assert.equal(error, null)
  65. assert.notEqual(sources.length, 0)
  66. assert.equal(sources.length, displays.length)
  67. for (let i = 0; i < sources.length; i++) {
  68. assert.equal(sources[i].display_id, displays[i].id)
  69. }
  70. done()
  71. })
  72. })
  73. })