2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2021-10-04 03:16:00 +00:00
|
|
|
import { screen, desktopCapturer, BrowserWindow } from 'electron/main';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { delay, ifdescribe, ifit } from './lib/spec-helpers';
|
|
|
|
import { emittedOnce } from './lib/events-helpers';
|
2022-03-29 14:05:08 +00:00
|
|
|
|
2023-01-25 21:01:25 +00:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2016-04-30 11:51:09 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2018-06-13 16:15:34 +00:00
|
|
|
|
2020-06-01 14:13:47 +00:00
|
|
|
ifdescribe(!process.arch.includes('arm') && process.platform !== 'win32')('desktopCapturer', () => {
|
|
|
|
if (!features.isDesktopCapturerEnabled()) {
|
|
|
|
// This condition can't go the `ifdescribe` call because its inner code
|
|
|
|
// it still executed, and if the feature is disabled some function calls here fail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2020-06-01 14:13:47 +00:00
|
|
|
|
2019-09-03 06:59:54 +00:00
|
|
|
before(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-03-20 20:28:31 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
2020-06-01 14:13:47 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
after(closeAllWindows);
|
2019-09-03 06:59:54 +00:00
|
|
|
|
2022-03-29 15:57:21 +00:00
|
|
|
it('should return a non-empty array of sources', async () => {
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] });
|
|
|
|
expect(sources).to.be.an('array').that.is.not.empty();
|
|
|
|
});
|
2019-01-18 23:29:32 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
it('throws an error for invalid options', async () => {
|
|
|
|
const promise = desktopCapturer.getSources(['window', 'screen'] as any);
|
|
|
|
await expect(promise).to.be.eventually.rejectedWith(Error, 'Invalid options');
|
|
|
|
});
|
2019-01-18 23:29:32 +00:00
|
|
|
|
2022-03-29 15:57:21 +00:00
|
|
|
it('does not throw an error when called more than once (regression)', async () => {
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources1 = await desktopCapturer.getSources({ types: ['window', 'screen'] });
|
|
|
|
expect(sources1).to.be.an('array').that.is.not.empty();
|
2016-01-19 18:57:18 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources2 = await desktopCapturer.getSources({ types: ['window', 'screen'] });
|
|
|
|
expect(sources2).to.be.an('array').that.is.not.empty();
|
|
|
|
});
|
2016-04-27 20:32:14 +00:00
|
|
|
|
2022-03-29 15:57:21 +00:00
|
|
|
it('responds to subsequent calls of different options', async () => {
|
2021-10-04 03:16:00 +00:00
|
|
|
const promise1 = desktopCapturer.getSources({ types: ['window'] });
|
|
|
|
await expect(promise1).to.eventually.be.fulfilled();
|
2018-07-17 04:20:50 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
const promise2 = desktopCapturer.getSources({ types: ['screen'] });
|
|
|
|
await expect(promise2).to.eventually.be.fulfilled();
|
|
|
|
});
|
2018-04-09 05:43:35 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
// Linux doesn't return any window sources.
|
2022-03-29 15:57:21 +00:00
|
|
|
ifit(process.platform !== 'linux')('returns an empty display_id for window sources', async () => {
|
2021-10-04 03:16:00 +00:00
|
|
|
const w = new BrowserWindow({ width: 200, height: 200 });
|
|
|
|
await w.loadURL('about:blank');
|
2018-06-17 23:00:28 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources = await desktopCapturer.getSources({ types: ['window'] });
|
|
|
|
w.destroy();
|
|
|
|
expect(sources).to.be.an('array').that.is.not.empty();
|
|
|
|
for (const { display_id: displayId } of sources) {
|
|
|
|
expect(displayId).to.be.a('string').and.be.empty();
|
|
|
|
}
|
|
|
|
});
|
2020-05-21 00:25:49 +00:00
|
|
|
|
2022-03-29 15:57:21 +00:00
|
|
|
ifit(process.platform !== 'linux')('returns display_ids matching the Screen API', async () => {
|
2021-10-04 03:16:00 +00:00
|
|
|
const displays = screen.getAllDisplays();
|
|
|
|
const sources = await desktopCapturer.getSources({ types: ['screen'] });
|
|
|
|
expect(sources).to.be.an('array').of.length(displays.length);
|
2018-12-20 02:44:30 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
for (let i = 0; i < sources.length; i++) {
|
|
|
|
expect(sources[i].display_id).to.equal(displays[i].id.toString());
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-02-13 18:27:42 +00:00
|
|
|
|
|
|
|
it('disabling thumbnail should return empty images', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w2 = new BrowserWindow({ show: false, width: 200, height: 200, webPreferences: { contextIsolation: false } });
|
2020-03-20 20:28:31 +00:00
|
|
|
const wShown = emittedOnce(w2, 'show');
|
|
|
|
w2.show();
|
|
|
|
await wShown;
|
2019-02-13 18:27:42 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
const isEmpties: boolean[] = (await desktopCapturer.getSources({
|
|
|
|
types: ['window', 'screen'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
|
|
|
})).map(s => s.thumbnail.constructor.name === 'NativeImage' && s.thumbnail.isEmpty());
|
2019-09-03 06:59:54 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
w2.destroy();
|
|
|
|
expect(isEmpties).to.be.an('array').that.is.not.empty();
|
|
|
|
expect(isEmpties.every(e => e === true)).to.be.true();
|
|
|
|
});
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
it('getMediaSourceId should match DesktopCapturerSource.id', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
|
2020-03-20 20:28:31 +00:00
|
|
|
const wShown = emittedOnce(w, 'show');
|
|
|
|
const wFocused = emittedOnce(w, 'focus');
|
|
|
|
w.show();
|
|
|
|
w.focus();
|
|
|
|
await wShown;
|
|
|
|
await wFocused;
|
|
|
|
|
|
|
|
const mediaSourceId = w.getMediaSourceId();
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources = await desktopCapturer.getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.destroy();
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// TODO(julien.isorce): investigate why |sources| is empty on the linux
|
|
|
|
// bots while it is not on my workstation, as expected, with and without
|
|
|
|
// the --ci parameter.
|
|
|
|
if (process.platform === 'linux' && sources.length === 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
it.skip('desktopCapturer.getSources returned an empty source list');
|
|
|
|
return;
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(sources).to.be.an('array').that.is.not.empty();
|
2019-08-15 06:51:15 +00:00
|
|
|
const foundSource = sources.find((source) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
return source.id === mediaSourceId;
|
|
|
|
});
|
|
|
|
expect(mediaSourceId).to.equal(foundSource!.id);
|
|
|
|
});
|
2019-08-15 06:51:15 +00:00
|
|
|
|
2021-08-13 20:23:56 +00:00
|
|
|
it('getSources should not incorrectly duplicate window_id', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, width: 100, height: 100, webPreferences: { contextIsolation: false } });
|
|
|
|
const wShown = emittedOnce(w, 'show');
|
|
|
|
const wFocused = emittedOnce(w, 'focus');
|
|
|
|
w.show();
|
|
|
|
w.focus();
|
|
|
|
await wShown;
|
|
|
|
await wFocused;
|
|
|
|
|
|
|
|
// ensure window_id isn't duplicated in getMediaSourceId,
|
|
|
|
// which uses a different method than getSources
|
|
|
|
const mediaSourceId = w.getMediaSourceId();
|
|
|
|
const ids = mediaSourceId.split(':');
|
|
|
|
expect(ids[1]).to.not.equal(ids[2]);
|
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
const sources = await desktopCapturer.getSources({
|
2021-08-13 20:23:56 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
|
|
|
});
|
|
|
|
w.destroy();
|
|
|
|
|
|
|
|
// TODO(julien.isorce): investigate why |sources| is empty on the linux
|
|
|
|
// bots while it is not on my workstation, as expected, with and without
|
|
|
|
// the --ci parameter.
|
|
|
|
if (process.platform === 'linux' && sources.length === 0) {
|
|
|
|
it.skip('desktopCapturer.getSources returned an empty source list');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(sources).to.be.an('array').that.is.not.empty();
|
|
|
|
for (const source of sources) {
|
|
|
|
const sourceIds = source.id.split(':');
|
|
|
|
expect(sourceIds[1]).to.not.equal(sourceIds[2]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-29 14:05:08 +00:00
|
|
|
it('moveAbove should move the window at the requested place', async () => {
|
2019-08-15 06:51:15 +00:00
|
|
|
// DesktopCapturer.getSources() is guaranteed to return in the correct
|
|
|
|
// z-order from foreground to background.
|
2020-03-20 20:28:31 +00:00
|
|
|
const MAX_WIN = 4;
|
2022-03-29 14:05:08 +00:00
|
|
|
const wList: BrowserWindow[] = [];
|
|
|
|
|
|
|
|
const destroyWindows = () => {
|
|
|
|
for (const w of wList) {
|
|
|
|
w.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-15 06:51:15 +00:00
|
|
|
try {
|
2022-03-29 14:05:08 +00:00
|
|
|
for (let i = 0; i < MAX_WIN; i++) {
|
|
|
|
const w = new BrowserWindow({ show: false, width: 100, height: 100 });
|
2020-03-20 20:28:31 +00:00
|
|
|
wList.push(w);
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(wList.length).to.equal(MAX_WIN);
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// Show and focus all the windows.
|
2022-03-29 14:05:08 +00:00
|
|
|
for (const w of wList) {
|
|
|
|
const wShown = emittedOnce(w, 'show');
|
2020-03-20 20:28:31 +00:00
|
|
|
const wFocused = emittedOnce(w, 'focus');
|
2022-03-29 14:05:08 +00:00
|
|
|
|
|
|
|
w.show();
|
2020-03-20 20:28:31 +00:00
|
|
|
w.focus();
|
2022-03-29 14:05:08 +00:00
|
|
|
|
|
|
|
await wShown;
|
2020-03-20 20:28:31 +00:00
|
|
|
await wFocused;
|
2022-03-29 14:05:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 06:51:15 +00:00
|
|
|
// At this point our windows should be showing from bottom to top.
|
|
|
|
|
|
|
|
// DesktopCapturer.getSources() returns sources sorted from foreground to
|
|
|
|
// background, i.e. top to bottom.
|
2021-10-04 03:16:00 +00:00
|
|
|
let sources = await desktopCapturer.getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// TODO(julien.isorce): investigate why |sources| is empty on the linux
|
|
|
|
// bots while it is not on my workstation, as expected, with and without
|
|
|
|
// the --ci parameter.
|
|
|
|
if (process.platform === 'linux' && sources.length === 0) {
|
2022-03-29 14:05:08 +00:00
|
|
|
destroyWindows();
|
2020-03-20 20:28:31 +00:00
|
|
|
it.skip('desktopCapturer.getSources returned an empty source list');
|
|
|
|
return;
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(sources).to.be.an('array').that.is.not.empty();
|
|
|
|
expect(sources.length).to.gte(MAX_WIN);
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// Only keep our windows, they must be in the MAX_WIN first windows.
|
2020-03-20 20:28:31 +00:00
|
|
|
sources.splice(MAX_WIN, sources.length - MAX_WIN);
|
|
|
|
expect(sources.length).to.equal(MAX_WIN);
|
|
|
|
expect(sources.length).to.equal(wList.length);
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// Check that the sources and wList are sorted in the reverse order.
|
2022-03-29 14:05:08 +00:00
|
|
|
// If they're not, skip remaining checks because either focus or
|
|
|
|
// window placement are not reliable in the running test environment.
|
|
|
|
const wListReversed = wList.slice().reverse();
|
|
|
|
const proceed = sources.every(
|
2020-03-20 20:28:31 +00:00
|
|
|
(source, index) => source.id === wListReversed[index].getMediaSourceId());
|
2022-03-29 14:05:08 +00:00
|
|
|
if (!proceed) return;
|
|
|
|
|
|
|
|
// Move windows so wList is sorted from foreground to background.
|
|
|
|
for (const [i, w] of wList.entries()) {
|
|
|
|
if (i < wList.length - 1) {
|
|
|
|
const next = wList[wList.length - 1];
|
|
|
|
w.focus();
|
|
|
|
w.moveAbove(next.getMediaSourceId());
|
|
|
|
// Ensure the window has time to move.
|
|
|
|
await delay(2000);
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
2022-03-29 14:05:08 +00:00
|
|
|
}
|
2019-08-15 06:51:15 +00:00
|
|
|
|
2021-10-04 03:16:00 +00:00
|
|
|
sources = await desktopCapturer.getSources({
|
2019-08-15 06:51:15 +00:00
|
|
|
types: ['window'],
|
|
|
|
thumbnailSize: { width: 0, height: 0 }
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2022-03-29 14:05:08 +00:00
|
|
|
|
|
|
|
sources.splice(MAX_WIN, sources.length);
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(sources.length).to.equal(MAX_WIN);
|
|
|
|
expect(sources.length).to.equal(wList.length);
|
2019-08-15 06:51:15 +00:00
|
|
|
|
|
|
|
// Check that the sources and wList are sorted in the same order.
|
2022-03-29 14:05:08 +00:00
|
|
|
for (const [index, source] of sources.entries()) {
|
|
|
|
const wID = wList[index].getMediaSourceId();
|
|
|
|
expect(source.id).to.equal(wID);
|
|
|
|
}
|
2019-08-15 06:51:15 +00:00
|
|
|
} finally {
|
2022-03-29 14:05:08 +00:00
|
|
|
destroyWindows();
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|