api-desktop-capturer-spec.ts 9.3 KB

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