api-desktop-capturer-spec.ts 8.7 KB

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