api-desktop-capturer-spec.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { screen, desktopCapturer, BrowserWindow } from 'electron/main';
  2. import { expect } from 'chai';
  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. // Regression test - see https://github.com/electron/electron/issues/43002
  125. it('does not affect window resizable state', async () => {
  126. w.resizable = false;
  127. const wShown = once(w, 'show');
  128. w.show();
  129. await wShown;
  130. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
  131. expect(sources).to.be.an('array').that.is.not.empty();
  132. expect(w.resizable).to.be.false();
  133. });
  134. it('moveAbove should move the window at the requested place', async () => {
  135. // DesktopCapturer.getSources() is guaranteed to return in the correct
  136. // z-order from foreground to background.
  137. const MAX_WIN = 4;
  138. const wList: BrowserWindow[] = [];
  139. const destroyWindows = () => {
  140. for (const w of wList) {
  141. w.destroy();
  142. }
  143. };
  144. try {
  145. for (let i = 0; i < MAX_WIN; i++) {
  146. const w = new BrowserWindow({ show: false, width: 100, height: 100 });
  147. wList.push(w);
  148. }
  149. expect(wList.length).to.equal(MAX_WIN);
  150. // Show and focus all the windows.
  151. for (const w of wList) {
  152. const wShown = once(w, 'show');
  153. const wFocused = once(w, 'focus');
  154. w.show();
  155. w.focus();
  156. await wShown;
  157. await wFocused;
  158. }
  159. // At this point our windows should be showing from bottom to top.
  160. // DesktopCapturer.getSources() returns sources sorted from foreground to
  161. // background, i.e. top to bottom.
  162. let sources = await desktopCapturer.getSources({
  163. types: ['window'],
  164. thumbnailSize: { width: 0, height: 0 }
  165. });
  166. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  167. // bots while it is not on my workstation, as expected, with and without
  168. // the --ci parameter.
  169. if (process.platform === 'linux' && sources.length === 0) {
  170. destroyWindows();
  171. it.skip('desktopCapturer.getSources returned an empty source list');
  172. return;
  173. }
  174. expect(sources).to.be.an('array').that.is.not.empty();
  175. expect(sources.length).to.gte(MAX_WIN);
  176. // Only keep our windows, they must be in the MAX_WIN first windows.
  177. sources.splice(MAX_WIN, sources.length - MAX_WIN);
  178. expect(sources.length).to.equal(MAX_WIN);
  179. expect(sources.length).to.equal(wList.length);
  180. // Check that the sources and wList are sorted in the reverse order.
  181. // If they're not, skip remaining checks because either focus or
  182. // window placement are not reliable in the running test environment.
  183. const wListReversed = wList.slice().reverse();
  184. const proceed = sources.every(
  185. (source, index) => source.id === wListReversed[index].getMediaSourceId());
  186. if (!proceed) return;
  187. // Move windows so wList is sorted from foreground to background.
  188. for (const [i, w] of wList.entries()) {
  189. if (i < wList.length - 1) {
  190. const next = wList[wList.length - 1];
  191. w.focus();
  192. w.moveAbove(next.getMediaSourceId());
  193. // Ensure the window has time to move.
  194. await setTimeout(2000);
  195. }
  196. }
  197. sources = await desktopCapturer.getSources({
  198. types: ['window'],
  199. thumbnailSize: { width: 0, height: 0 }
  200. });
  201. sources.splice(MAX_WIN, sources.length);
  202. expect(sources.length).to.equal(MAX_WIN);
  203. expect(sources.length).to.equal(wList.length);
  204. // Check that the sources and wList are sorted in the same order.
  205. for (const [index, source] of sources.entries()) {
  206. const wID = wList[index].getMediaSourceId();
  207. expect(source.id).to.equal(wID);
  208. }
  209. } finally {
  210. destroyWindows();
  211. }
  212. });
  213. });