api-desktop-capturer-spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { expect } from 'chai';
  2. import { screen, desktopCapturer, BrowserWindow } from 'electron/main';
  3. import { once } from 'node:events';
  4. import { setTimeout } from 'node:timers/promises';
  5. import { ifdescribe, ifit } from './lib/spec-helpers';
  6. import { closeAllWindows } from './lib/window-helpers';
  7. ifdescribe(!process.arch.includes('arm') && process.platform !== 'win32')('desktopCapturer', () => {
  8. let w: BrowserWindow;
  9. before(async () => {
  10. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  11. await w.loadURL('about:blank');
  12. });
  13. after(closeAllWindows);
  14. it('should return a non-empty array of sources', async () => {
  15. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
  16. expect(sources).to.be.an('array').that.is.not.empty();
  17. });
  18. it('throws an error for invalid options', async () => {
  19. const promise = desktopCapturer.getSources(['window', 'screen'] as any);
  20. await expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options');
  21. });
  22. it('does not throw an error when called more than once (regression)', async () => {
  23. const sources1 = await desktopCapturer.getSources({ types: ['window', 'screen'] });
  24. expect(sources1).to.be.an('array').that.is.not.empty();
  25. const sources2 = await desktopCapturer.getSources({ types: ['window', 'screen'] });
  26. expect(sources2).to.be.an('array').that.is.not.empty();
  27. });
  28. it('responds to subsequent calls of different options', async () => {
  29. const promise1 = desktopCapturer.getSources({ types: ['window'] });
  30. await expect(promise1).to.eventually.be.fulfilled();
  31. const promise2 = desktopCapturer.getSources({ types: ['screen'] });
  32. await expect(promise2).to.eventually.be.fulfilled();
  33. });
  34. // Linux doesn't return any window sources.
  35. ifit(process.platform !== 'linux')('returns an empty display_id for window sources', async () => {
  36. const w = new BrowserWindow({ width: 200, height: 200 });
  37. await w.loadURL('about:blank');
  38. const sources = await desktopCapturer.getSources({ types: ['window'] });
  39. w.destroy();
  40. expect(sources).to.be.an('array').that.is.not.empty();
  41. for (const { display_id: displayId } of sources) {
  42. expect(displayId).to.be.a('string').and.be.empty();
  43. }
  44. });
  45. ifit(process.platform !== 'linux')('returns display_ids matching the Screen API', async () => {
  46. const displays = screen.getAllDisplays();
  47. const sources = await desktopCapturer.getSources({ types: ['screen'] });
  48. expect(sources).to.be.an('array').of.length(displays.length);
  49. for (const [i, source] of sources.entries()) {
  50. expect(source.display_id).to.equal(displays[i].id.toString());
  51. }
  52. });
  53. it('disabling thumbnail should return empty images', async () => {
  54. const w2 = new BrowserWindow({ show: false, width: 200, height: 200, webPreferences: { contextIsolation: false } });
  55. const wShown = once(w2, 'show');
  56. w2.show();
  57. await wShown;
  58. const isEmpties: boolean[] = (await desktopCapturer.getSources({
  59. types: ['window', 'screen'],
  60. thumbnailSize: { width: 0, height: 0 }
  61. })).map(s => s.thumbnail.constructor.name === 'NativeImage' && s.thumbnail.isEmpty());
  62. w2.destroy();
  63. expect(isEmpties).to.be.an('array').that.is.not.empty();
  64. expect(isEmpties.every(e => e === true)).to.be.true();
  65. });
  66. it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
  67. const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
  68. const wShown = once(w, 'show');
  69. const wFocused = once(w, 'focus');
  70. w.show();
  71. w.focus();
  72. await wShown;
  73. await wFocused;
  74. const mediaSourceId = w.getMediaSourceId();
  75. const sources = await desktopCapturer.getSources({
  76. types: ['window'],
  77. thumbnailSize: { width: 0, height: 0 }
  78. });
  79. w.destroy();
  80. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  81. // bots while it is not on my workstation, as expected, with and without
  82. // the --ci parameter.
  83. if (process.platform === 'linux' && sources.length === 0) {
  84. it.skip('desktopCapturer.getSources returned an empty source list');
  85. return;
  86. }
  87. expect(sources).to.be.an('array').that.is.not.empty();
  88. const foundSource = sources.find((source) => {
  89. return source.id === mediaSourceId;
  90. });
  91. expect(mediaSourceId).to.equal(foundSource!.id);
  92. });
  93. it('getSources should not incorrectly duplicate window_id', async () => {
  94. const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
  95. const wShown = once(w, 'show');
  96. const wFocused = once(w, 'focus');
  97. w.show();
  98. w.focus();
  99. await wShown;
  100. await wFocused;
  101. // ensure window_id isn't duplicated in getMediaSourceId,
  102. // which uses a different method than getSources
  103. const mediaSourceId = w.getMediaSourceId();
  104. const ids = mediaSourceId.split(':');
  105. expect(ids[1]).to.not.equal(ids[2]);
  106. const sources = await desktopCapturer.getSources({
  107. types: ['window'],
  108. thumbnailSize: { width: 0, height: 0 }
  109. });
  110. w.destroy();
  111. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  112. // bots while it is not on my workstation, as expected, with and without
  113. // the --ci parameter.
  114. if (process.platform === 'linux' && sources.length === 0) {
  115. it.skip('desktopCapturer.getSources returned an empty source list');
  116. return;
  117. }
  118. expect(sources).to.be.an('array').that.is.not.empty();
  119. for (const source of sources) {
  120. const sourceIds = source.id.split(':');
  121. expect(sourceIds[1]).to.not.equal(sourceIds[2]);
  122. }
  123. });
  124. it('moveAbove should move the window at the requested place', async () => {
  125. // DesktopCapturer.getSources() is guaranteed to return in the correct
  126. // z-order from foreground to background.
  127. const MAX_WIN = 4;
  128. const wList: BrowserWindow[] = [];
  129. const destroyWindows = () => {
  130. for (const w of wList) {
  131. w.destroy();
  132. }
  133. };
  134. try {
  135. for (let i = 0; i < MAX_WIN; i++) {
  136. const w = new BrowserWindow({ show: false, width: 100, height: 100 });
  137. wList.push(w);
  138. }
  139. expect(wList.length).to.equal(MAX_WIN);
  140. // Show and focus all the windows.
  141. for (const w of wList) {
  142. const wShown = once(w, 'show');
  143. const wFocused = once(w, 'focus');
  144. w.show();
  145. w.focus();
  146. await wShown;
  147. await wFocused;
  148. }
  149. // At this point our windows should be showing from bottom to top.
  150. // DesktopCapturer.getSources() returns sources sorted from foreground to
  151. // background, i.e. top to bottom.
  152. let sources = await desktopCapturer.getSources({
  153. types: ['window'],
  154. thumbnailSize: { width: 0, height: 0 }
  155. });
  156. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  157. // bots while it is not on my workstation, as expected, with and without
  158. // the --ci parameter.
  159. if (process.platform === 'linux' && sources.length === 0) {
  160. destroyWindows();
  161. it.skip('desktopCapturer.getSources returned an empty source list');
  162. return;
  163. }
  164. expect(sources).to.be.an('array').that.is.not.empty();
  165. expect(sources.length).to.gte(MAX_WIN);
  166. // Only keep our windows, they must be in the MAX_WIN first windows.
  167. sources.splice(MAX_WIN, sources.length - MAX_WIN);
  168. expect(sources.length).to.equal(MAX_WIN);
  169. expect(sources.length).to.equal(wList.length);
  170. // Check that the sources and wList are sorted in the reverse order.
  171. // If they're not, skip remaining checks because either focus or
  172. // window placement are not reliable in the running test environment.
  173. const wListReversed = wList.slice().reverse();
  174. const proceed = sources.every(
  175. (source, index) => source.id === wListReversed[index].getMediaSourceId());
  176. if (!proceed) return;
  177. // Move windows so wList is sorted from foreground to background.
  178. for (const [i, w] of wList.entries()) {
  179. if (i < wList.length - 1) {
  180. const next = wList[wList.length - 1];
  181. w.focus();
  182. w.moveAbove(next.getMediaSourceId());
  183. // Ensure the window has time to move.
  184. await setTimeout(2000);
  185. }
  186. }
  187. sources = await desktopCapturer.getSources({
  188. types: ['window'],
  189. thumbnailSize: { width: 0, height: 0 }
  190. });
  191. sources.splice(MAX_WIN, sources.length);
  192. expect(sources.length).to.equal(MAX_WIN);
  193. expect(sources.length).to.equal(wList.length);
  194. // Check that the sources and wList are sorted in the same order.
  195. for (const [index, source] of sources.entries()) {
  196. const wID = wList[index].getMediaSourceId();
  197. expect(source.id).to.equal(wID);
  198. }
  199. } finally {
  200. destroyWindows();
  201. }
  202. });
  203. });