api-desktop-capturer-spec.ts 9.1 KB

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