api-desktop-capturer-spec.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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('enabling thumbnail should return non-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 isNonEmpties: boolean[] = (await desktopCapturer.getSources({
  59. types: ['window', 'screen'],
  60. thumbnailSize: { width: 100, height: 100 }
  61. })).map(s => s.thumbnail.constructor.name === 'NativeImage' && !s.thumbnail.isEmpty());
  62. w2.destroy();
  63. expect(isNonEmpties).to.be.an('array').that.is.not.empty();
  64. expect(isNonEmpties.every(e => e === true)).to.be.true();
  65. });
  66. it('disabling thumbnail should return empty images', async () => {
  67. const w2 = new BrowserWindow({ show: false, width: 200, height: 200, webPreferences: { contextIsolation: false } });
  68. const wShown = once(w2, 'show');
  69. w2.show();
  70. await wShown;
  71. const isEmpties: boolean[] = (await desktopCapturer.getSources({
  72. types: ['window', 'screen'],
  73. thumbnailSize: { width: 0, height: 0 }
  74. })).map(s => s.thumbnail.constructor.name === 'NativeImage' && s.thumbnail.isEmpty());
  75. w2.destroy();
  76. expect(isEmpties).to.be.an('array').that.is.not.empty();
  77. expect(isEmpties.every(e => e === true)).to.be.true();
  78. });
  79. it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
  80. const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
  81. const wShown = once(w, 'show');
  82. const wFocused = once(w, 'focus');
  83. w.show();
  84. w.focus();
  85. await wShown;
  86. await wFocused;
  87. const mediaSourceId = w.getMediaSourceId();
  88. const sources = await desktopCapturer.getSources({
  89. types: ['window'],
  90. thumbnailSize: { width: 0, height: 0 }
  91. });
  92. w.destroy();
  93. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  94. // bots while it is not on my workstation, as expected, with and without
  95. // the --ci parameter.
  96. if (process.platform === 'linux' && sources.length === 0) {
  97. it.skip('desktopCapturer.getSources returned an empty source list');
  98. return;
  99. }
  100. expect(sources).to.be.an('array').that.is.not.empty();
  101. const foundSource = sources.find((source) => {
  102. return source.id === mediaSourceId;
  103. });
  104. expect(mediaSourceId).to.equal(foundSource!.id);
  105. });
  106. it('getSources should not incorrectly duplicate window_id', async () => {
  107. const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
  108. const wShown = once(w, 'show');
  109. const wFocused = once(w, 'focus');
  110. w.show();
  111. w.focus();
  112. await wShown;
  113. await wFocused;
  114. // ensure window_id isn't duplicated in getMediaSourceId,
  115. // which uses a different method than getSources
  116. const mediaSourceId = w.getMediaSourceId();
  117. const ids = mediaSourceId.split(':');
  118. expect(ids[1]).to.not.equal(ids[2]);
  119. const sources = await desktopCapturer.getSources({
  120. types: ['window'],
  121. thumbnailSize: { width: 0, height: 0 }
  122. });
  123. w.destroy();
  124. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  125. // bots while it is not on my workstation, as expected, with and without
  126. // the --ci parameter.
  127. if (process.platform === 'linux' && sources.length === 0) {
  128. it.skip('desktopCapturer.getSources returned an empty source list');
  129. return;
  130. }
  131. expect(sources).to.be.an('array').that.is.not.empty();
  132. for (const source of sources) {
  133. const sourceIds = source.id.split(':');
  134. expect(sourceIds[1]).to.not.equal(sourceIds[2]);
  135. }
  136. });
  137. // Regression test - see https://github.com/electron/electron/issues/43002
  138. it('does not affect window resizable state', async () => {
  139. w.resizable = false;
  140. const wShown = once(w, 'show');
  141. w.show();
  142. await wShown;
  143. const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
  144. expect(sources).to.be.an('array').that.is.not.empty();
  145. expect(w.resizable).to.be.false();
  146. });
  147. it('moveAbove should move the window at the requested place', async () => {
  148. // DesktopCapturer.getSources() is guaranteed to return in the correct
  149. // z-order from foreground to background.
  150. const MAX_WIN = 4;
  151. const wList: BrowserWindow[] = [];
  152. const destroyWindows = () => {
  153. for (const w of wList) {
  154. w.destroy();
  155. }
  156. };
  157. try {
  158. for (let i = 0; i < MAX_WIN; i++) {
  159. const w = new BrowserWindow({ show: false, width: 100, height: 100 });
  160. wList.push(w);
  161. }
  162. expect(wList.length).to.equal(MAX_WIN);
  163. // Show and focus all the windows.
  164. for (const w of wList) {
  165. const wShown = once(w, 'show');
  166. const wFocused = once(w, 'focus');
  167. w.show();
  168. w.focus();
  169. await wShown;
  170. await wFocused;
  171. }
  172. // At this point our windows should be showing from bottom to top.
  173. // DesktopCapturer.getSources() returns sources sorted from foreground to
  174. // background, i.e. top to bottom.
  175. let sources = await desktopCapturer.getSources({
  176. types: ['window'],
  177. thumbnailSize: { width: 0, height: 0 }
  178. });
  179. // TODO(julien.isorce): investigate why |sources| is empty on the linux
  180. // bots while it is not on my workstation, as expected, with and without
  181. // the --ci parameter.
  182. if (process.platform === 'linux' && sources.length === 0) {
  183. destroyWindows();
  184. it.skip('desktopCapturer.getSources returned an empty source list');
  185. return;
  186. }
  187. expect(sources).to.be.an('array').that.is.not.empty();
  188. expect(sources.length).to.gte(MAX_WIN);
  189. // Only keep our windows, they must be in the MAX_WIN first windows.
  190. sources.splice(MAX_WIN, sources.length - MAX_WIN);
  191. expect(sources.length).to.equal(MAX_WIN);
  192. expect(sources.length).to.equal(wList.length);
  193. // Check that the sources and wList are sorted in the reverse order.
  194. // If they're not, skip remaining checks because either focus or
  195. // window placement are not reliable in the running test environment.
  196. const wListReversed = wList.slice().reverse();
  197. const proceed = sources.every(
  198. (source, index) => source.id === wListReversed[index].getMediaSourceId());
  199. if (!proceed) return;
  200. // Move windows so wList is sorted from foreground to background.
  201. for (const [i, w] of wList.entries()) {
  202. if (i < wList.length - 1) {
  203. const next = wList[wList.length - 1];
  204. w.focus();
  205. w.moveAbove(next.getMediaSourceId());
  206. // Ensure the window has time to move.
  207. await setTimeout(2000);
  208. }
  209. }
  210. sources = await desktopCapturer.getSources({
  211. types: ['window'],
  212. thumbnailSize: { width: 0, height: 0 }
  213. });
  214. sources.splice(MAX_WIN, sources.length);
  215. expect(sources.length).to.equal(MAX_WIN);
  216. expect(sources.length).to.equal(wList.length);
  217. // Check that the sources and wList are sorted in the same order.
  218. for (const [index, source] of sources.entries()) {
  219. const wID = wList[index].getMediaSourceId();
  220. expect(source.id).to.equal(wID);
  221. }
  222. } finally {
  223. destroyWindows();
  224. }
  225. });
  226. });