api-desktop-capturer-spec.ts 9.6 KB

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